Awk's use case

Source: Internet
Author: User

How to use

awk [-F Separator] ' handling of operations ' is the file name to be processed

It can be separated by a space without a separator.

Three ways to execute awk

1. Command mode

2.Shell scripting #!/bin/awk on the first line

3. Insert all awk commands into a single file using the awk - F awk command file with the processing file

A few simple examples

1. Get the most recently logged in user name simple print

Last - N 5 | awk ' {print '} '

2. Get/etc/passwd account Specify the delimiter

$ indicates that all the domains , $ represents the first domain , $n represents the first N a domain

CAT/ETC/PASSWD |awk-f ': ' {print $} '

can also

Awk-f ': ' {print $} '/etc/passwd

Print multiple columns

awk-f ': ' {print $ \ t ' $7} '/etc/passwd

3. Use Begin end  

Example 1

CAT/ETC/PASSWD |awk-f ': ' BEGIN {print ' Name,shell '} {print $ ', ' $7} END {print ' Blue,/bin/nosh '} '

Example 2

Awk-f ":" ' begin{count=0;} {name[count]=$1;count++} End{for (i=0;i<count;i++) print I,name[i]} '/etc/passwd

4. Using the regular

Example 1 View all columns that contain ysql

Awk-f ":" ' begin{count=0;} /ysql/{name[count]=$1;count++}end{for (i=0;i<count;i++) print I,name[i]} '/etc/passwd

Example 2 View all columns beginning with Ysql

Awk-f ":" ' begin{count=0;} /^ysql/{name[count]=$1;count++}end{for (i=0;i<count;i++) print I,name[i]} '/etc/passwd

Example 3 View all columns ending in Ysql

Awk-f ":" ' begin{count=0;} /ysql$/{name[count]=$1;count++}end{for (i=0;i<count;i++) print I,name[i]} '/etc/passwd

Note that the regular must be placed behind the begin


Awk's use case

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.