awk:the most powerful utility tool in shell world.
2004-09-24 10:15
681 查看
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
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
相关文章推荐
- THE MOST EXPENSIVE PIANO IN THE WORLD
- 世界上最远的距离(The most distant way in the world)
- the most distant way in the world(Indian Tagore)
- The 10 most powerful women in Silicon Valley
- The most distant way in the world
- The most happiest country in the world
- The Adobe Photoshop Layers Book: Harnessing Photoshop's Most Powerful Tool, covers Photoshop CS3
- Linux Annoyances for Geeks : Getting the Most Flexible System in the World Just the Way You Want It
- The most distant way in the world
- [导入]Vista, the evil OS in the world.
- How Rich Are You in the World?
- FASHION DNA: STRUCTURAL FEATURE MAPPING IN THE WORLD OF RETAIL
- Nothing in the world can take the place of Persistence
- Testing in the Agile World (Final Part - 4)
- 12 Programming Languages in the Modern Web/Mobile World of 2014
- What is the world like in the coming ten years?
- The Farthest Distance in the World
- The war of the world-----The war in my heart
- Most Advanced Grinding Mill in the Mining MachinesEconomic development upgrades technology – MTW European Trapezium grin
- 数据库调优:The most important factor in making a database application fast is its basic design.