CSDN博客

img symield

awk:the most powerful utility tool in shell world.

发表于2004/9/24 10:15:00  690人阅读

1)   FORMAT:   awk 'pattern' filename
                          awk '{action}' filename
                          awk  'pattern {action}' filename

(a)  awk '/Sally/ {print $1} filename

(b)  ls -l |awk '$1 ~ /^d/ {print $0}'

(c)  ls -l |awk '$1 !~ /^d/ {print $0}'

(d)  awk ' /^(no|so)/' datafile

(e) awk -F: '$3==100||$4>=150' filename

 

2) send the shell's variable to nawk

for example:

Origin=ls -l

OriginB= `ls / |nawk -v swap="$Origin"  '{print swap}'

3)nawk '$2 > 5 && $2 <= 15' filename

4)nawk 'BEGIN{while (getline < "/etc/passwd" > 0 )lc++; print lc}'

5)nawk '{print $1, $2 | "sort –r +1 –2 +0 –1 "}' names

6)cat /etc/passwd |awk -F: '{print NF}'

7)nawk '/^north/{count += 1; print count}' datafile

8)nawk '{ i = 1; while ( i <= NF ) { print NF, $i ; i++ } }' filename

9)nawk '{ for( i = 1; i <= NF; i++) print NF,$i }' filex


10)FORMAT
sub (regular expression, substitution string);
sub (regular expression, substitution string, target string)

nawk '{sub(/Mac/, "MacIntosh"); print}' filename
nawk '{sub(/Mac/, "MacIntosh", $1); print}' filename

11)nawk '{ print index("hollow", "low") }' filename

12)nawk '{ print length("hello") }' filename

13)nawk ' { print substr("Santa Claus", 7, 6 )} ' filename

14)nawk 'END{start=match("Good ole USA", /[A–Z]+$/); print start}'/
filename

15)nawk 'END{start=match("Good ole USA", /[A–Z]+$/);/
print RSTART, RLENGTH}' filename

16)awk 'BEGIN{split("12/25/2001",date,"/");print date[2]}' filename

阅读全文
0 0

相关文章推荐

img
取 消
img