shell and sed Introduction to text processing --gawk

gawk program

GNU version of the original Unix awk program in the program when gawk. In gawk programming language, you can do the following things:

  • Define variables to hold the data
  • And using the arithmetic operators to process the data string
  • Using a structured programming concepts (such as if-then statements and loops) to increase the processing logic of process data.
  • By extracting the data elements in the data file, to rearrange or format, generate a formatted report
    1.gawk command format
    basic format gawk procedure are as follows:
    gawk Options Program File
    gawk options are as follows:
    | Options | Description |
    | - | - |
    | -F FS | specified line in the data field of the divided field separator |
    | -f file | read from the specified file |
    | -v var = value | a variable defined in its default program gawk value |
    | specify the maximum number of fields in the data file to be processed in | | -mf N
    | -mr N | Specifies the maximum number of rows of data in the data file |
    | -W keyword | gawk specify compatibility mode or warning level |

2. Use a variable data field
default, gawk following variables will be assigned to its data fields found in the line of text:

  • $ 0 represents the entire line of text;
  • $ 1 represents the first line of text in a data field
  • $ 2 represents the second line of text data field
  • $ n-th data field representative of n lines of text
    The following example represents a read from a text file, and displays the value of a data field
[root@ommleft zd]# cat data2.txt
One line of test text.
Two line of test text.
Three line of test text.
[root@ommleft zd]# gawk '{print $1}' data2.txt
One
Two
Three

Using field separator to read the file, you may be used to specify options -F

[root@ommleft zd]# gawk -F: '{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown

3. Use multiple commands from the script
to the script on the command line using multiple commands, you can put a semicolon can be between commands.

[root@ommleft zd]# echo "My name is Rich"|gawk '{$4="Christine";print $0}'
My name is Christine

4. read the program from the file
gawk editor allows a program stored in a file, and referenced in the command line.

[root@ommleft zd]# more script.gawk
{print $1 "'s home directory is " $6}
[root@ommleft zd]# gawk -F: -f script.gawk /etc/passwd
root's home directory is /root
bin's home directory is /bin
daemon's home directory is /sbin
adm's home directory is /var/adm
lp's home directory is /var/spool/lpd

BEGIN 5.gawk
BEGIN keyword gawk forces in the script specified gawk after the BEGIN keyword before reading data.

[root@ommleft zd]# gawk 'BEGIN{print "Hello World!"}'
Hello World!

Read text and display

[root@ommleft zd]# cat data3.txt
Line 1
Line 2
Line 3
root@ommleft zd]# gawk 'BEGIN {print "The data3 file contents:"}{print $0}' data3.txt
The data3 file contents:
Line 1
Line 2
Line 3

6.gawk END
similar keyword BEGIN, END keyword allows you to specify a script, gawk will be performed after reading data.

[root@ommleft zd]# gawk 'BEGIN{print "The data3 file contents:"}
{print $0}
END{print "End of File"}' data3.txt
The data3 file contents:
Line 1
Line 2
Line 3
End of File

sed Editor

sed editor is referred to as stream editor (stream editor), sed editor performs the following operations:
1) the first line of data read from the input.
2) match the command data according to the editor provided
3) according to commands to modify data stream
) to output a new data 4 to STDOUT
Sed command format is as follows:
Sed Options Script File
Sed command options are as follows:
| Options | Description |
| -e command | Adds the specified command to the existing command |
| -f file | Add the file specified in the command to the existing command |
| -n | no command output, use the print command completed output |

1. In the definition of the command line editor commands

[root@ommleft zd]# echo "This is a test"|sed 's/test/trial/' 
This is a trial

May also be treated, STDOUT output file data, but does not modify the data file

[root@ommleft zd]# cat data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
[root@ommleft zd]# sed 's/dog/cat/' data1.txt
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.

2. At the command line using the multiple editing commands
to be executed sed multiple commands on the command line, just use -e option on it.

[root@ommleft zd]# sed -e 's/brown/green/;s/dog/cat/' data1.txt
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.

3. Read editor commands from a file

[root@ommleft zd]# cat script.sed
s/brown/green/
s/fox/elephant/
s/dog/cat/
[root@ommleft zd]# sed -f script.sed data1.txt
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
Published 75 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/zhengdong12345/article/details/101266083