awk Command Learning

Source: Internet
Author: User

It is said that some people use this outfit to force. Ha ha. I just have to master the basics.

awk ' Condition 1{action 1} Condition 2{action 2} ... ' Filename

Condition (Pattern):

-General use of relationship expressions as criteria

-You can use functions, etc.

Actions (Action):

-Formatted output

-Process Control Statements

Test Document:

1. Output 2nd, line 4

awk ' {print $ "\ T" $4} ' student.txt Name    Markfurong    fengj    -cang    

2. Output Df-h 1th, 3 lines

DF awk ' {print "\ t" $ $} ' Filesystem    Used/dev/mapper/vg_myfuture-lv_root    18G    13Gtmpfs    228K/dev/sda1    29M/ Dev/sr0    4. 4G

And cut doesn't make it.

[Email protected] ~]#DF-H |Cut-F1,3Filesystem Size used Avail use%mounted on/dev/mapper/vg_myfuture-lv_root 18G3.7G 13G at% /tmpfs 491M 228K 491M1%/dev/SHM/DEV/SDA1 477M 29M 424M7% /Boot/dev/sr04.4G4.4G0  -%/media/centos_6.6_final

3. Get the usage of/DEV/SDA1:

DF grep " /dev/sda1 " awk ' {print $} ' Cut " % " 1 7

4.BEGIN relationship expression, you can continue an action only if the relationship expression is satisfied first.

awk ' Begin{print "This is a test"}{print "\ T" $4} ' Student.txt This is a testname    Markfurong    fengj    -cang    

The 5.END will match after all the inputs have been processed.

awk ' End{print "This is a test"}{print "\ T" $4} ' student.txt Name    Markfurong    fengj    cang     Test

6. Extract the user name and ID

cat /etc/passwdgrepawk'begin{fs= ': "}{print" \ T "$"  Root    0Zabbix    Hadoop    501 user1     502

7. Extract the names of people with a score greater than 70

Cat grep awk ' $4>=70 {print $} ' Furongcang

8.awk by default with a space delimiter, use the '-f ' flag in awk to specify your delimiter

[Email protected] ~]#  echo'onemississippi,two mississippi,three mississippi,four Mississippi'awk'{print $4}' four Mississippi 

9. As long as the last field. The $NF variable built into awk represents the number of fields so that you can use it to grab the last element:

Echo ' One, three four ' awk ' {print $NF} '

The second-lowest field:

Cat awk ' {print $ (NF-1)} ' genderfff

Intermediate fields:

Cat awk ' {print $ ((NF/2) +1)} ' genderfff

Cross-line status of 10.awk

Echo ' One 1\ntwo 2 ' awk ' {sum+=$2} END {print sum} ' 3

awk Command Learning

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.