StarCoder17
StarCoder17

Reputation: 175

awk: how to do a pattern matching with whitespace

I am fairly new to awk, my intention is to perform pattern matching for a following contents in file. The pattern should get any line having vdd* or vss* I tried following in awk for pattern vdd, but it didn't work.

if(($1~"inout/svdd") || ($1~"input/svdd") || ($1~"output/svdd")) {
do_something
}

Sample input:

  input tprImeasbuf;
  output ibias7;
  inout vssd;
  output vddpminvref;
  input disablevdda1v8;
  output ibiaspowerreferencescp5;
  output vddaok;
  input tprImeasPLL;
  inout mtp_isense;
  output ibiaspowerreferencescp3;
  output enablelevelshifts;
  output poff;
  inout vddp;
  input ResGainSel412;
  output icasc_ch5;
  inout Rref;
  output icasc_ch3;
  output ibiasshutdowncomparator;
  output vdddok;
  input tpr_vbgbufout;
  output iref3v3_ch4;
  inout atb5;
  input tprIrefcurbuf;
  output ibiaspowerreference4;
  input tpr_vdddcompextref;
  input tprIrefcurPLL;

Expected output:

vssd
vddpminvref
vddaok
vddp
vdddok

Upvotes: 0

Views: 231

Answers (3)

oliv
oliv

Reputation: 13249

You probably want to use grep for such simple pattern matching:

grep -o -E ' v(dd|ss)[^ ;]*' file

-o is for printing the matching regex (and not the whole line).

-E enables the extended regex (ERE).

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203219

$ awk '$2~/^v(ss|dd)/{sub(/;\r$/,"",$2); print $2}' file
vssd
vddpminvref
vddaok
vddp
vdddok

The \r is to handle the fact your posted sample input uses \r\n newlines. If that's not the case in your real input then just remove the \r. I highly recommend running dos2unix on your file to strip those \rs before running awk or any other UNIX tool on it.

Upvotes: 3

stack0114106
stack0114106

Reputation: 8711

Is this what you are expecting.

$ awk -F" " ' $0~/vdd|vss/ { print } ' in_file
  inout vssd;
  output vddpminvref;
  input disablevdda1v8;
  output vddaok;
  inout vddp;
  output vdddok;
  input tpr_vdddcompextref;

$

EDIT1:

$ awk -F" " ' $1~/input|inout|output/ && $2~/^(vdd|vss)/ { print } ' in_file
  inout vssd;
  output vddpminvref;
  output vddaok;
  inout vddp;
  output vdddok;

Upvotes: 1

Related Questions