abc.dat
tty cpu
tin tout us sy wt id
0 0 7 3 19 71
extended device statistics
r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device
0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0
0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx
I want to skip first 5 line headers. Then select rows with column 11 starts with 'aaaaaa1'.
# Print first 5 rows + all rows from 6th row whose column 11 starts with
# 'aaaaaa1' string.
cat abc.dat | awk '(NR<6) {print}; (NR>5), $11 ~ /^aaaaaa1/'
Apparently last part of awk is not filtering 6th row even though its 11 th column does not start with 'aaaaaa1' string.
Please correct it..
kchinnam:
abc.dat
tty cpu
tin tout us sy wt id
0 0 7 3 19 71
extended device statistics
r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device
0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0
0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx
I want to skip first 5 line headers. Then select rows with column 11 starts with 'aaaaaa1'.
...
$
$
$ cat abc.dat
tty cpu
tin tout us sy wt id
0 0 7 3 19 71
extended device statistics
r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device
0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0
0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx
$
$
$ perl -lane '$.>5 && print if $F[10] =~ /^aaaaaa1/' abc.dat
0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx
$
$
$ awk 'NR>5 && $11 ~ /^aaaaaa1/' abc.dat
0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx
$
$
Ok, seems like I misread it.
If you want to print the first 5 lines and apply the condition from line 6 onwards then -
$
$
$ perl -lane 'print if $.>5 && $F[10] =~ /^aaaaaa1/ || $.<=5' abc.dat
tty cpu
tin tout us sy wt id
0 0 7 3 19 71
extended device statistics
r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device
0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx
$
$
$
$ awk 'NR<=5 || NR>5 && $11 ~ /^aaaaaa1/' abc.dat
tty cpu
tin tout us sy wt id
0 0 7 3 19 71
extended device statistics
r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device
0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx
$
$
tyler_durden
Thanks. That works.. I will go with last command..
I also want to use a variable in place of search string 'aaaaaa1'. For some reason awk's variable substitution does not seem to working,,
# since /bin/awk is older version,, it does not recongnize -v flag, so I am using awk from /usr/xpg4/bin/ library.
var1='aaaaaa1'; /usr/xpg4/bin/awk -v var2=$var1 'NR<=5 || NR>5 && $11 ~ /^var2/' abc.dat
tty cpu
tin tout us sy wt id
0 0 7 3 19 71
extended device statistics
r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device
<nothing>
Please let me know how to correct the above awk to recognize string variable in place of regex expression..
Try:
var1='aaaaaa1'; /usr/xpg4/bin/awk -v var2=$var1 'NR<=5 || NR>5 && $11 ~ "^"var2' abc.dat