Tuesday, April 19, 2011

Shell Script Notes

Question: How to do this in shell script
I have a txt file aa.txt like below:

100 | 200
11 | 22 | 33 | 44
12 | 23 | 35 | 55
14 | 24 | 36 | 56
200 | 300
21 | 22 | 330 | 44
22 | 23 | 315 | 55
24 | 55 | 361 | 56

I like to choose all the lines with 4 fields and collect the second
field of the 4 fields

i.e., discard all lines only with 2 fields, and take the second field of
the remaining,

the output should be
22
23
24
22
23
55

Answer:
grep ".*|.*|.*|.*" aa.txt |awk '{print $3}'

grep "^[0-9]\+ | [0-9]\+ | [0-9]\+ | [0-9]\+$" a | awk '{print $3}'

awk '{if (NF>4) print $3}' temp

awk -F'|' 'NF>4{print $2}' temp
yours are wrong :) should be 'NF>3'
Dashagen's was correct coz awk's default separator is space/tab.


Do it in the Q way,

d[;1] @/: (til count d) except where max each null d: flip
("IIII";"|") 0: `:aa.txt
Perl version:
perl -anle 'print @F[2] if scalar @F>3' aa.txt


Think:
if we only are interested the lines with 2 fields and like to print their
2nd field, what can we do?

No comments: