Shell practice (1)-awk

Source: Internet
Author: User

Recently, I learned the content of shell script writing and read the script easily. But in fact, there are still some difficulties in hand, so I decided to find some examples to practice, after all, there is a saying that is very good: "I have to get a glimpse of it on paper, and I am sure I want to do it." It makes sense that I have recently realized it! Through practice, it is also an advanced step in the "shell knowledge point supplement" section! Conquer shell. Let's talk about Python !!

1. awk

Awk usage: awk 'pattern' {action }'

Awk "/keyword/" File Name

LOONG:/home/Yee/shell #Awk '/root/' xAA
Display lines with root in the xAA File

Root: X: 0: 0: Root:/root:/bin/bash

LOONG:/home/Yee/shell #Awk '/3/' xAADisplay rows with 3 in the xAA File
SYS: X: 3: 3: SYS:/dev:/bin/sh
Sync: X: 4: 65534: Sync:/bin/Sync
Proxy: X: 13: 13: proxy:/bin/sh
WWW-data: X: 33: 33: www-data:/var/www:/bin/sh
Backup: X: 34: 34: Backup:/var/backups:/bin/sh

LOONG:/home/Yee/shell # Cat last.txt Use Case File Content
Root pts/6 172.16.7.94 wed Oct 31 :09 still logged in
Yee pts/5 172.16.7.97 wed Oct 31 :09 still logged in
Yee pts/1 172.16.7.95 wed Oct 31 10: 06 still logged in
Root pts/6 172.16.7.94 Tue Oct 30)
Yee pts/5 172.16.3.89 Tue Oct 30)
Yee pts/1 172.16.7.94 Tue Oct 30)
Loongson pts/6 172.16.7.94 mon Oct 29)
Yee pts/5 172.16.3.89 mon Oct 29)
Loongson pts/1 172.16.7.94 mon Oct 29)
Yee pts/5 172.16.7.94 Fri Oct 26)

Wtmp begins wed Oct 10 09:34:03 2012

LOONG:/home/Yee/shell #Awk '$1 = "yee"' last.txtDisplay the row whose first variable is Yee. Note that double quotation marks must be included.
Yee pts/5 172.16.7.97 wed Oct 31 :09 still logged in
Yee pts/1 172.16.7.95 wed Oct 31 10: 06 still logged in
Yee pts/5 172.16.3.89 Tue Oct 30)
Yee pts/1 172.16.7.94 Tue Oct 30)
Yee pts/5 172.16.3.89 mon Oct 29)
Yee pts/5 172.16.7.94 Fri Oct 26)

LOONG:/home/Yee/shell #Awk '$1> $ 2' last.txtThe first line of the string greater than the second is displayed;
Root pts/6 172.16.7.94 wed Oct 31 :09 still logged in
Yee pts/5 172.16.7.97 wed Oct 31 :09 still logged in
Yee pts/1 172.16.7.95 wed Oct 31 10: 06 still logged in
Root pts/6 172.16.7.94 Tue Oct 30)
Yee pts/5 172.16.3.89 Tue Oct 30)
Yee pts/1 172.16.7.94 Tue Oct 30)
Yee pts/5 172.16.3.89 mon Oct 29)
Yee pts/5 172.16.7.94 Fri Oct 26)
Wtmp begins wed Oct 10 09:34:03 2012

LOONG:/home/Yee/shell #Awk '{print NR, NF, $1, $ NF}' last.txtDisplays the current row number, number of fields (separated by spaces), and the first and last fields (strings) of each row)
1 10 root in
2 10 Yee in
3 10 Yee in
4 10 root)
5 10 Yee (11: 06)
6 10 Yee (11: 04)
7 10 loongson)
8 10 Yee (10: 08)
9 10 loongson (10: 31)
10 10 Yee (06:33)
11 0
12 7 wtmp 2012
LOONG:/home/Yee/shell #
LOONG:/home/Yee/shell #Awk '/loongson/{print $1, $2 + 23}' last.txtIt is unclear why the first and second fields of the row matching the file F are overwritten with 23?
Loongson 23
Loongson 23
LOONG:/home/Yee/shell #Awk '/loongson/{print $1, $2}' last.txt
Loongson pts/6
Loongson pts/1

LOONG:/home/Yee/shell # awk '/loongson/{print $1 $2} 'last.txt
Loongsonpts/6
Loongsonpts/1

LOONG:/home/Yee/shell #Awk '/loongson/{print $1 $2}' last.txtComma (,) indicates that two fields are separated by spaces.
Loongsonpts/6
Loongsonpts/1
LOONG:/home/Yee/shell #

LOONG:/home/Yee/shell # DF
File System 1 K-block used available % mount point
/Dev/sda5 20641788 7509808 12083340 39%/
Tmpfs 253472 16 253456 1%/lib/init/RW
Udev 253472 656 252816 1%/dev
Tmpfs 253472 16 253456 1%/dev/SHM
/Dev/sda6 126849780 2437976 117968136 3%/home
SHM 253472 253472 0 100%/tmp
LOONG:/home/Yee/shell #DF | awk '$4> 10000000'The input is obtained by using the pipeline operator. For example, a row with 4th fields meeting the conditions is displayed.
File System 1 K-block used available % mount point
/Dev/sda5 20641788 7509808 12083340 39%/
/Dev/sda6 126849780 2437976 117968136 3%/home
LOONG:/home/Yee/shell #
LOONG:/home/Yee/shell #Awk-F "/" '{print $1}' last.txtOutput The first custom domain according to the new separator "/"
Root PTS
Yee PTS
Yee PTS
Root PTS
Yee PTS
Yee PTS
Loongson PTS
Yee PTS
Loongson PTS
Yee PTS

Wtmp begins wed Oct 10 09:34:03 2012 not found/, all as the first field output

LOONG:/home/Yee/shell #Awk 'in in {FS = "/"} {print $1} 'last.txtOutput The first custom domain according to the new separator "/". method 2 sets the input Separator
Root PTS
Yee PTS
Yee PTS
Root PTS
Yee PTS
Yee PTS
Loongson PTS
Yee PTS
Loongson PTS
Yee PTS

Wtmp begins wed Oct 10 09:34:03 2012
LOONG:/home/Yee/shell #Awk 'in in {FS = "."} {print $1} 'last.txt
Output The first custom domain according to the new separator "/". method 2 sets the input Separator
Root pts/6 172
Yee pts/4 172
Yee pts/1 172
Root pts/6 172
Yee pts/4 172
Yee pts/1 172
Loongson pts/6 172
Yee pts/4 172
Loongson pts/1 172
Yee pts/4 172

Wtmp begins wed Oct 10 09:34:03 2012
LOONG:/home/Yee/shell #

LOONG:/home/Yee/shell #Es = "/"
LOONG:/home/Yee/shell #Awk-F $ es '{print $1}' last.txtUse the elasticsearch environment variable value as the separator.
Root PTS
Yee PTS
Yee PTS
Root PTS
Yee PTS
Yee PTS
Loongson PTS
Yee PTS
Loongson PTS
Yee PTS

Wtmp begins wed Oct 10 09:34:03 2012
LOONG:/home/Yee/shell #

LOONG:/home/Yee/shell #Awk-F' [/] ''{print $1} 'last.txt

Root PTS
Yee PTS
Yee PTS
Root PTS
Yee PTS
Yee PTS
Loongson PTS
Yee PTS
Loongson PTS
Yee PTS

Wtmp begins wed Oct 10 09:34:03 2012

LOONG:/home/Yee/shell #Awk-F' [: \ t] ''{print $1} 'last.txt
Displays the specified output according to the value of the regular expression.
Root
Yee
Yee
Root
Yee
Yee
Loongson
Yee
Loongson
Yee

Wtmp
LOONG:/home/Yee/shell #

LOONG:/home/Yee/shell #Awk '$1 ~ /Root/
{Print $1} 'last.txt

Root
Root
LOONG:/home/Yee/shell #Awk '$1 ~ /Root/{print $3} 'last.txtThe first field in the file matches the root row and the third field is output.
172.16.7.94
172.16.7.94

LOONG:/home/Yee/shell #Awk '{print
($1> $2? "High" $1: "low" $1)} 'last.txt
(Expression 1? Expression 2: expression 3) is equivalent:
If (expression 1)
Expression 2
Else
Expression 3
If the High root condition is true, expression 2 is executed; otherwise, expression 3 is executed.
High Yee
High Yee
High Root
High Yee
High Yee
Low loongson
High Yee
Low loongson
High Yee
Low
High wtmp
LOONG:/home/Yee/shell #
LOONG:/home/Yee/shell #Awk '/172.16.7.94/{$3 = 1000; print}' last.txtAssign a value to the variable $3 after finding the matching row and print the row. You can also specify the print variable for Print $3.
Root pts/6 1000 wed Oct 31 still logged in
Root pts/6 1000 Tue Oct 30)
Yee pts/1 1000 Tue Oct 30)
Loongson pts/6 1000 mon Oct 29)
Loongson pts/1 1000 mon Oct 29)
Yee pts/5 1000 Fri Oct 26)
LOONG:/home/Yee/shell #

LOONG:/home/Yee/shell #Awk '/Yee/{count ++;} end {print "Yee was found" count "times"}' last.txtEnd indicates processing after all input rows are processed
Yee was found 6 times
LOONG:/home/Yee/shell #
LOONG:/home/Yee/shell #Awk
'Gsub (/\./, "dot"); end {print $3} 'last.txt
The gsub function is replaced by a string, and the result is output.
Root pts/6 172dot16dot7dot94 wed Oct 31 10:09 still logged in
Yee pts/5 172dot16dot7dot97 wed Oct 31 10:09 still logged in
Yee pts/1 172dot16dot7dot95 wed Oct 31 10:06 still logged in
Root pts/6 172dot16dot7dot94 Tue Oct 30)
Yee pts/5 172dot16dot3dot89 Tue Oct 30)
Yee pts/1 172dot16dot7dot94 Tue Oct 30)
Loongson pts/6 172dot16dot7dot94 mon Oct 29)
Yee pts/5 172dot16dot3dot89 mon Oct 29)
Loongson pts/1 172dot16dot7dot94 mon Oct 29)
Yee pts/5 172dot16dot7dot94 Fri Oct 26)
Wed
LOONG:/home/Yee/shell #
To call environment variables in an awk, they must not appear in single quotes:
Flag = ABCD
Awk '{print' $ flag'} 'is incorrectly used. The quotation marks are not paired and the result depends on the environment.
Awk '{print "$ flag"}' returns $ flag
Awk "{print '$ flag'}" returns $ flag
Awk "{print \" $ flag \ "}" returns ABCD
The difference between single double quotation marks is that shell directly transmits the content in single quotation marks to awk without explanation, and then the content in double quotation marks to awk.

From the above example, we can see that the main usage of awk is to modify some content of the line and string based on specific judgment characters or definitions. The main methods are as follows:

(1) Use built-in parameters such as NF and FS to set conditions;

(2) Use/character/to set conditions;

(3) Use a regular expression [character] to set conditions

(4) use =, if, else; while, auto-increment; greater than, less than; for loop, etc.

Finally, specify the output.

More methods reference: http://www.chinaunix.net/old_jh/24/691456.html

Related Article

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.