user
user

Reputation: 503

How to capture header of file, then perform further text processing on original file?

I would like to capture the first 8 lines of the following file, then capture all fields where NPU # is equal to 7 or 3, and NPU core is equal to 1.

RP/0/RP0/CPU0:xxxxx#show controllers npu voq-usage interface all instance all location 0/0/CPU0
33144 Tue Jun  4 xxxx
33145 -------------------------------------------------------------------
33146 Node ID: 0/0/CPU0
33147 Intf         Intf     NPU NPU  PP   Sys   VOQ   Flow   VOQ    Port
33148 name         handle    #  core Port Port  base  base   port   speed
33149 (hex)                                     type
33150 ----------------------------------------------------------------------
33151 Hu0/0/2/1    210       0   1   21    21   1024   4152 local   100G
33152 Hu0/0/2/47   218       7   0    1   701   1032   4152 local   100G
33153 Hu0/0/2/46   220       7   0    5   705   1040   4216 local   100G
33154 Hu0/0/2/45   228       7   0    9   709   1048   4280 local   100G
33155 Hu0/0/2/44   230       3   1   17   717   1056   4152 local   100G

I'm able to do this with the following disparate awk commands:

awk 'NR<9' filename.txt
awk '($4==7||$4==3) && $5==1' filename.txt

Can I get some advice on how I might join these awk statement together? Or maybe even a bash-ism that would be more elegant to achieve this?

Thanks,

Upvotes: 0

Views: 62

Answers (4)

potong
potong

Reputation: 58401

This might work for you (GNU sed):

sed -E '1,8b;/^(\S+\s+){3}[37]\s+1\s/!d' file

Print lines 1 to 8 and any lines where the 4th field is either 3 or 7 and 5th field is 1.

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 203493

awk '(NR<9) || (($4~/^[73]$/) && ($5==1))' filename.txt

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133508

Could you please try 1 more way.

awk 'NR<=9{print;next} ($4==7||$4==3) && $5==1'  Input_file

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626804

I suggest using a || operator and group the second condition:

awk 'NR<9 || (($4==7||$4==3) && $5==1)' filename.txt > newfilename.txt

See the online demo:

s="RP/0/RP0/CPU0:xxxxx#show controllers npu voq-usage interface all instance all location 0/0/CPU0
33144 Tue Jun  4 xxxx
33145 -------------------------------------------------------------------
33146 Node ID: 0/0/CPU0
33147 Intf         Intf     NPU NPU  PP   Sys   VOQ   Flow   VOQ    Port
33148 name         handle    #  core Port Port  base  base   port   speed
33149 (hex)                                     type
33150 ----------------------------------------------------------------------
33151 Hu0/0/2/1    210       0   1   21    21   1024   4152 local   100G
33152 Hu0/0/2/47   218       7   0    1   701   1032   4152 local   100G
33153 Hu0/0/2/46   220       7   0    5   705   1040   4216 local   100G
33154 Hu0/0/2/45   228       7   0    9   709   1048   4280 local   100G
33155 Hu0/0/2/44   230       3   1   17   717   1056   4152 local   100G"

awk 'NR<9 || (($4==7||$4==3) && $5==1)'  <<< "$s"

Output:

RP/0/RP0/CPU0:xxxxx#show controllers npu voq-usage interface all instance all location 0/0/CPU0
33144 Tue Jun  4 xxxx
33145 -------------------------------------------------------------------
33146 Node ID: 0/0/CPU0
33147 Intf         Intf     NPU NPU  PP   Sys   VOQ   Flow   VOQ    Port
33148 name         handle    #  core Port Port  base  base   port   speed
33149 (hex)                                     type
33150 ----------------------------------------------------------------------
33155 Hu0/0/2/44   230       3   1   17   717   1056   4152 local   100G

Upvotes: 2

Related Questions