One Linux command a day (20): sed command

This article is an original article of Joshua 317. Please note: reprinted from Joshua 317 blog One Linux command a day (20): sed command - Joshua 317 blog

1, Introduction

sed command in Linux system is a powerful text processing tool. sed (Stream EDitor) is a stream file editor that processes one line at a time. During processing, the currently processed line is stored in a temporary buffer called "Pattern Space", and then the contents in the buffer are processed with the sed command. After processing, the contents of the buffer are sent to the screen, and then the next line is processed until the end of the file.

The contents of the file will not change unless you use the - i option. sed can process and edit text files according to the instructions of the script. sed is mainly used to edit one or more files, simplify the repeated operation of files, or write conversion programs.

The function of SED is similar to that of awk. The difference is that sed is simple, and the function of column processing is worse. The function of awk is complex, and the function of column processing is more powerful.

2, Format description

sed [OPTION]... {script-only-if-no-other-script} [input-file]...

Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  --follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode
  -b, --binary
                 does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (
                 open files in binary mode (CR+LFs are not treated specially))
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -r, --regexp-extended
                 use extended regular expressions in the script.
  -s, --separate
                 consider files as separate rather than as a single continuous
                 long stream.
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
  -z, --null-data
                 separate lines by NUL characters
  --help
                 display this help and exit
  --version
                 output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

Among them, OPTION is the command OPTION, script only if no other script is the processing action, multiple parameters can be specified by - e, input file is the input file, and multiple parameters can be specified.

3, Option description

Parameter Description:
-e<script>or--expression=<script> appoint sed Action to the specified in the options script To process the input text file, which can be composed of multiple-e Specify multiple actions
-f<script file>or--file=<script file> As specified in the options script File to process the input text file. Directly sed The actions of are written in a file,-f filename Can be run filename Internal sed Action;
-h or--help Displays help.
-n or--quiet or--silent Show only script Results after processing. sed In the general usage of, all from STDIN The data will generally be printed to the terminal if-n After, only after sed The row with special treatment will be listed
-r or--regexp-extended: sed Support extended regular expressions(The default is the base regular expression)
-i : Directly modify the contents of the read file instead of outputting it to the terminal.
-V or--version Displays version information.

Action description:
a : newly added, a Can be followed by strings, and these strings will appear on a new line(Current next line)~
c : replace, c Can be followed by strings, which can replace n1,n2 Between the lines!
d : Delete, because it is delete, so d There is usually no thump at the back;
i : Insert, i Can be followed by strings, and these strings will appear on a new line(Current previous line);
p : Printing, that is, printing a selected data. usually p Meeting with parameters sed -n Run together~
s : Replace, you can directly replace the work! Usually this s The action of can be matched with formal representation! Example 1,10s/old/new/g That's it!

4, Command function

sed is mainly used to automatically edit one or more files, simplify the repeated operation of files, write conversion programs, etc.

5, Common usage

1. Data search and processing

1.1. Search for lines with Joshua 317 keyword in test.txt

nl test.txt |sed '/joshua317/p'

#results:
# nl test.txt |sed '/joshua317/p'
     1  hello
     2  joshua317
     2  joshua317
     3  i
     4  love
     5  China
     6  ,
     7  joshua317
     7  joshua317
     8  my
     9  name
    10  yes

If Joshua 317 finds it, it will output matching rows in addition to all rows.

1.2. When - n is used, only the lines containing the template will be printed.

nl test.txt |sed -n '/joshua317/p'

#results:
# nl test.txt |sed -n '/joshua317/p'
     2  joshua317
     7  joshua317

1.3. Display lines 3 and 6 in the test.txt file.

nl test.txt | sed -n '3,6p'

#results:
# nl test.txt | sed -n '3,6p'
     3  i
     4  love
     5  China
     6  ,

1.4. Search and delete the data, delete all the lines in test.txt containing Joshua 317, and output other lines

nl test.txt | sed '/joshua317/d'

#results:
# nl test.txt | sed '/joshua317/d'
     1  hello
     3  i
     4  love
     5  China
     6  ,
     8  my
     9  name
    10  yes

1.5. Delete matching string

# nl test.txt | sed  's/joshua317//g'
     1  hello
     2
     3  i
     4  love
     5  China
     6  ,
     7
     8  my
     9  name
    10  yes

1.6. Search and replace

# nl test.txt | sed  's/joshua317/joshua319/g'
     1  hello
     2  joshua319
     3  i
     4  love
     5  China
     6  ,
     7  joshua319
     8  my
     9  name
    10  yes

2. Add line

2.1. After the second line, add "what's your name?"

nl test.txt |sed "2a what's you name?"#results:#nl test.txt |sed "2a what's you name?"     1  hello     2  joshua317what's you name?     3  i     4  love     5  China     6  ,     7  joshua317     8  my     9  name    10  yes

2.2. Add "what's your name?"

nl test.txt |sed "2i what's you name?"#results:# nl test.txt |sed "2i what's you name?"     1  hellowhat's you name?     2  joshua317     3  i     4  love     5  China     6  ,     7  joshua317     8  my     9  name    10  yes

2.3. Add two lines after the second line, "what's your name?" and "nice to meet you"

nl test.txt |sed "2i what's you name?\nnice to meet you"#results:# nl test.txt |sed "2i what's you name?\nnice to meet you"     1  hellowhat's you name?nice to meet you     2  joshua317     3  i     4  love     5  China     6  ,     7  joshua317     8  my     9  name    10  yes

2.4. Use backslash \ to separate each line, you can separate one command from multiple lines on the command line

#results:# nl test.txt |sed "2i what's you name?\> \n\> nice to meet you \> "     1  hellowhat's you name?nice to meet you      2  joshua317     3  i     4  love     5  China     6  ,     7  joshua317     8  my     9  name    10  yes

3. Delete row

3.1 list the contents of test.txt and print the line number. At the same time, please delete lines 1 ~ 3.

nl test.txt | sed '1,3d'#results:# nl test.txt | sed '1,3d'     4  love     5  China     6  ,     7  joshua317     8  my     9  name    10  yes

Note: originally, sed -e should be issued. When there is only one action, it is OK without - E, but when there is more than one action, you must use the - e option to specify the action.

3.2. Just delete line 2

nl test.txt | sed '2d'#results:# nl test.txt | sed '2d'     1  hello     3  i     4  love     5  China     6  ,     7  joshua317     8  my     9  name    10  yes

3.3. To delete line 3 to the last line

nl test.txt | sed '3,$d'#results:# nl test.txt | sed '3,$d'     1  hello     2  joshua317

4. Replace line

4.1 replace lines 2-5 with "you are Joshua 317".

nl test.txt | sed '2,5c you are joshua317'#results:# nl test.txt | sed '2,5c you are joshua317'     1  helloyou are joshua317     6  ,     7  joshua317     8  my     9  name    10  yes

4.2. Replace the search content.

nl test.txt | sed  's/joshua317/joshua319/g'#results:# nl test.txt | sed 's/joshua317/joshua319/g'     1  hello     2  joshua319     3  i     4  love     5  China     6  ,     7  joshua319     8  my     9  name    10  yes

5. Multipoint editing

5.1. A sed command, delete the data from the third line to the end of test.txt, and replace Joshua 317 with Joshua 319

nl test.txt | sed -e '3,$d' -e 's/joshua317/joshua319/'#results:# nl test.txt | sed -e '3,$d' -e 's/joshua317/joshua319/'     1  hello     2  joshua319

explain:

-e means multi-point editing. The first editing command deletes the data from the third line to the end of test.txt, and the second command searches for Joshua 317 and replaces Joshua 319.

6. Direct modification of documents

The - i option of sed can directly modify the file content without using pipeline command or data flow redirection! However, since this action will directly modify the original file, please do not take the system configuration to test, and be careful when using it.

6.1. Replace "Joshua 317" with "Joshua 319" in the test.txt file

sed -i 's/joshua317/joshua319/g' test.txt #results:# sed -i 's/joshua317/joshua319/g' test.txt [root@service-01 ~]# cat test.txt hellojoshua319iloveChina,joshua319mynameyes

6.2. Add "welcome to China" in the last line of test.txt file.

sed -i '$a welcome to China' test.txt #results:# sed -i '$a welcome to China' test.txt [root@service-01 ~]# cat test.txt hellojoshua319iloveChina,joshua319mynameyeswelcome to China

6.3. Delete the line containing "Joshua 319" in the test.txt file

sed -i '/joshua319/d' test.txt #results:# sed -i '/joshua319/d' test.txt [root@service-01 ~]# cat test.txt helloiloveChina,mynameyeswelcome to China

This article is an original article of Joshua 317. Please note: reprinted from Joshua 317 blog One Linux command a day (20): sed command - Joshua 317 blog

Tags: Linux Operation & Maintenance

Posted on Wed, 29 Sep 2021 14:40:10 -0400 by jkkenzie