Detailed shell script (11)-awk text and data processing programming language

1. The awk command-a programming language for text and data processing

  • The awk command is a programming language used to process text and data under linux/unix.
  • And it supports advanced functions such as user-defined functions and dynamic regular expressions, and is a powerful programming tool under linux/unix.

1. Working principle

  • Read text line by line, separated by space or tab by default, save the separated fields to built-in variables, and execute editing commands according to the mode or condition.

  • The sed command is often used to process a whole line, while awk tends to divide a line into multiple "fields" and then process it. The reading of awk information is also read line by line, and the execution result can be printed and displayed with the field data through the print function. In the process of using the awk command, you can use the logical operators "&&" to mean "and", "||" to mean "or", and "!" to mean "not"; you can also perform simple mathematical operations, such as +,- , *, /, %, ^ represent addition, subtraction, multiplication, division, remainder and power respectively.

2. Command format

Insert picture description here

3. Common built-in variables of awk (can be used directly)

Built-in variables Description
FS Column separator. Specify the field separator for each line of text, the default is a space or a tab stop. Same as "-F"
NF The number of fields in the row currently being processed
NO The row number (ordinal number) of the row currently being processed
$0 The entire line content of the currently processed line
$n The nth field (column n) of the currently processed row
FILENAME File name being processed
RS Line separator. When awk reads data from a file, it will cut the data into many records according to the definition of RS, while awk only reads one record at a time for processing. The default value is'\n'

4. Output text by line

Insert picture description here

Insert picture description here

  • BEGIN mode means that before processing the specified text, you need to perform the action specified in the BEGIN mode; awk processes the specified text, and then executes the action specified in the END mode. The END{} statement block is often placed in printing Statements such as results.

5. Output text by field

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

6. Invoke Shell commands through pipes and double quotes

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

7、getline

  • When there is no redirection character "<" or "|" on the left and right of getline, getline acts on the current file, and reads the first line of the current file to the variable var or $0 followed by it; it should be noted that because awk has already processed the getline before A line is read, so the return result of getline is interlaced.
  • When there are redirection characters "<" or "|" on the left and right of getline, getline acts on the directional input file. Since the file is just opened and has not been read into a line by awk, it is only read by getline, then getline returns this The first line of the file, not every other line.

Insert picture description here

2. Simple example: extract the local IP address and perform binary conversion output

method one:

#获取IP
ifconfig | awk '(NR==2) {print $2}'

#分字段操作
ifconfig |awk '(NR==2) {print $2}' | awk 'BEGIN {RS="."} ; {print} ' 

#对分段后的IP地址赋值给一个变量
x=`ifconfig |awk '(NR==2) {print $2}' | awk 'BEGIN {RS="."} ; {print} '`

#进行外层循环,四次
for a in $x
do
     #进行内循环,选用减法进行循环,如果外层循环的值比被减数大,则取余数,并输出1;反之则输出0,且不取减法结果。   
     for ((i=1;i<=255;i+=$i))
     do
       m=$[128/$i]
       sum=$[$a-$m]
          if [ $a -ge $m ]
          then
          a=$[$a-$m]
          echo -n "1"  #不换行输出
          else
          echo -n "0"  #不换行输出
          fi
     done

echo  " "  #进行系统默认的空格换行操作
done   > 123.txt
      #进行结果的输出,并把结果中的空格全部替换成 "."号 

IP="`cat 123.txt`"
echo $IP | sed 's/ /./g'

Insert picture description here

Method two (function + interactive version)

#!/bin/bash
#定义一个函数,使用位置变量来进行操作
ip(){
    
          
x=`echo $1 | awk 'BEGIN {RS="."} ; {print} '`
for a in $x
do
    #进行内循环,选用减法进行循环,如果外层循环的值比被减数大,则取余数,并输出1;反之则输出0,且不取减法结果。
    for ((i=1;i<=255;i+=$i))
    do
      m=$[128/$i]
      sum=$[$a-$m]
      if [ $a -ge $m ]
      then
          a=$[$a-$m]
          echo -n "1" 
      else
      echo -n "0"  
      fi
    done

echo  -e "\t" 
done
}

##############################

read -p "请输入想要转换的IP地址:" p    #定义交互式变量P
list="`ip $p`"                       #把调用函数后得出的结果放入列表中
echo $list | sed 's/ /./g'            #输出列表,并对分隔符进行替换

Insert picture description here

Guess you like

Origin blog.csdn.net/Lucien010230/article/details/114854014