awk Utility 20 Example

Source: Internet
Author: User

1. function is to print all input lines
awk ' {print $} ' F Ilename
2. Print the input file line eighth
awk ' Nr==8{print $} ' F Ilename
3. Use awk to print the first field of all rows of a file
Awk-f: ' {print $} ' F Ilename
4. Print the total number of input lines
awk ' begin{num=0;} {num++;} End{print num} ' f ilename
awk ' End{print NR} ' F Ilename
5. Print the number of fields per row
Awk-f: ' {print nf,$0} ' F ilename
6. Print the value of the last field in the last line
Awk-f: ' End{print $NF} ' filename
7. The number of printed fields is greater than or equal to 4 rows
Awk-f: ' Nf>=4{print $} ' F Ilename
8. Print the total number of all fields in a file
Awk-f: ' Begin{count=0}{count=count+nf:}{print count} ' F Ilename
9. Print user name with UID within 30-40 range
Awk-f: ' $>=30&&s3<=40{print $ ' F ilename
10. Flashback all fields of the file
Awk-f: ' {for (i=nf;i>0:i--)} {if (i!=1} {printf "%s:", $i};} else{printf ("%s\n", $i);}
#!/bin/awk-f
begin{
F= ":"
}
{
for (i=nf;i>0;i--)
{
if (i!=1)

printf ("%s:", $i)

Else
{
printf ("%s\n", $i)
}
}
}
11. Print 5 to 26 lines
Awk-f: ' Nr>=5&&nr<=26{print} ' F ilename12. Add the title "document" to the top of the file
Awk ' Bengin{print ' Document}{print} ' f Ilename
13. Interlaced removal
Awk ' {if (nr%2==1) {print}} ' F Ilename
14. Total number of rows with more than 5 printed fields
Awk-f: ' Begin{num=0;} {if (nf>=5) {num++;}} END {print num;} ' f Ilename
15. Write an awk script to count the various types of files in/dev
#!/bin/awk-f
begin{
Ff=0
Dd=0
Bb=0
Cc=0
Pp=0
Ss=0
Ll=0
}
{
if ($ ~/^-/)

ff++

if ($ ~/^p/)

pp++

if ($ ~/^b/)

bb++

if ($ ~/^/)

Cc++

if ($ ~/^d/)

dd++

if ($ ~/^l/)

ll++

if ($ ~/^s/)
{s++

}
end{
Print "f ile:", F F
Print "directory:", DD
Print "Character:", CC
Print "Block:", BB
Print "Link:", ll
Print "Sockt:", SS
print "Pipe:", pp
}
16. Write an awk script that reads IP from a file and tests the IP address
#!/bin/awk-f
{
if (System ("Ping-c 1" >/dev/null) ==0)
{
Print $ YES
}
Else
{
Print $ NO
}
}
17. Write an awk script to read the file to add the user
#!/bin/awk-f
{
If (System ("Useradd" 2>/dev/null) ==0)
{
Print "User:" $ "Add"
}
Else
{
Print "User" $ "error"
}
}
18. Write an awk script that filters out legitimate IP addresses
#!/bin/awk-f
begin{
Fs= ":"
}
{
if (1>0&&$1<255&&$2>=0&&$2<=255$3>=0&&$3<=255$4>=0& &$4<=255{
Print $
}
}
19. Write an awk script that extracts the first word per line
#!/bin/awk-f
begin{
Fs= "[^a-z]+"
}
{f irst=0
F lag=0
for (i=1;i<=nf;i++)
{
If ($I!) =null}

if (f lag==0)
{
F irst=i
F lag=1
}

}
Print $f Irst
}
20. Write an awk script that prints the first and last words of each line
#!/bin/awk-f
begin{
Fs= "[^a-z]+"
}
{f irst=0
F lag=0
Last=0
for (i=1;i<=nf;i++)
{
If ($I!) =null}

Last=i
if (f lag==0)
{
F irst=i
F lag=1
}}
}
Print $f irst "" $last
}

This article is from the "Small Zheng Technology Blog" blog, please be sure to keep this source http://ko178.blog.51cto.com/220419/1765216

awk Utility 20 Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.