The awk command linux

A, awk built-in parameters

$ 0: represents the entire current line

$ 1: Each line of the first field

$ 2: Each line of the second field

$ N: n-th field in each row

awk parameters: delimiter

  -F separator set separator (space by default)  

Print a single field:

awk -F ':' '{print $3}'   /etc/passwd

Print multiple fields:

awk -F ':' '{print $1,$3}'  /etc/passwd

awk -F ':' '{printf("User:%s      UID:%s\n", $1,$3)}'  /etc/passwd

awk -F ':' '{print "User:" $1 "\t"UID:"$3"}'  /etc/passwd

  NR: Each line number

  NF: number of fields

awk -F ':' '{print NR,NF,FILENAME}' /ect/passwd

 

Case #:

1, an / etc / passwd user name ID is greater than the line number and user name 100

awk -F ":" '{if ($3>100) print NR,$1}' /etc/passwd

awk -F ":" '$3>100{print NR,$1}' /etc/passwd

2, find the log server IP: 43.226.164.120 date of visit.

sed -n '/43.226.164.120/p' assess_log | awk ' ' '{print substr($4,2)}' 

awk '/43.226.164.120/{print substr($4,2)}' assess_log

 

Two, awk determination logic formula:

  • ~ Matches the regular expression
  • ! ~ Do not match the regular expression
  • == equal
  • ! = Not equal
  • <Less than
  • > Greater than

awk -F ':'. '$ 1 ~ / ^ m * {print $ 1}' / etc / passwd print beginning m username

awk -F ':'!. '$ 1 ~ / ^ m * {print $ 1}' / etc / passwd not print beginning m username

Three, awk extended format

command extensions

   BEGIN{print "start"}pattern{awk命令}END{print "end"}

Case #: Tab displays the user name / etc / passwd line number per row, the number of columns per row, the corresponding row

awk -F ':' 'BEGIN {"User     Line    Col"}{print $1,NR,NF}END{print  "-------------"FILENAME"-------------"}'  /etc/passwd

Case #: Statistics files in the current folder in the folder occupied size /

ls -al | awk 'BEGIN{count=0}{count+=$5}END{print count}'

ls -al | awk 'BEGIN{count=0}{count+=$5}END{print count/1024/1024"M"}'

Statistics show # / etc total number / passwd account

awk -F ':'  'BEGIN{count=0}{count++}END{print count}' /etc/passwd

awk -F ':' 'BEGIN {count = 0} $ 1 ~ / ^ $ / {count ++} END {print count}' / etc / passwd exclude blank lines!

Statistics show # / etc username is greater than 100 / passwd in the UID

awk -F ':'  '$3>100{print $1}'  /etc/passwd

awk -F ':' 'BEGIN{count=0}$3>100{user[count++]=$1}END{for(i=0;i<count;i++) print i,user[i]}'  /etc/passwd

# Count the number of access_log log each IP appears

awk '{arr[$1]++}END{for(key in arr)print key,arr[key]}'  access_log

 

 

awk and sed contrast

1, awk and sed can handle text

2, awk focus on the complex logic

3, sed focus on the regular process

4, awk and sed can be used together

 

Interview Questions

1, modify all the files in a directory that contains the AAA under the file name to change the file name aaa []

find . -name "*aaa*"  -exec rename aaa AAA  { } \;

2, under the modified directory of all files containing aaa are changed to change the content of the document [AAA]

 sed -i 's / AAA / aaa / g' `grep AAA -rl.` backtick

sed itself does not change the content of the document, to use -i, and then look at the file

 

Guess you like

Origin www.cnblogs.com/wuzm/p/10956216.html