LINUX text processing command sed

sed

    sed is a line editor. It does not edit the original file by default, but only processes the data in the pattern space.

sed 'AddressCommand' file... # Perform command operations (Command) on which lines (Address) in the file file match the address

No spaces between AddressCommand

Address:

1. StartLine,EndLine

    For example: 1,100 processes 1 to 100 rows

2. /RegExp/ Regular Expression

    For example: /^root/ lines starting with root

3.  /pattern1/,/pattern2/

    For example: the first line that matches pattern1 starts, to the second line that matches pattern2, the lines between

4. StartLine,+N

    N lines starting from StarLine line, total N+1 lines

5.  $

    the last line

Command Commond

    d delete

    p print

    a \string Appends a new line (string) after the specified line

    i \string adds a new line (string) before the specified line

    r FILE Read the contents of the FILE file and add it to the line that matches the Address.

    w FILE Save the lines in file that match Address to the FILE file.

    s /pattern/string/ Replace the string matching pattern in the file with string, which defaults to the first string matched by pattern in each line.

modifier

    Modifiers must follow the command, for example: s /pattern/string/modifiers

    g global s /pattern/string/g global replacement

    i ignore case s /pattern/string/i ignore case when matching


example:

sed -n '20 ,30p'123.txt Print lines 20 to 30 in 123.txt, with -n is silent output, that is, only the processed result is output, and the content of the entire pattern space is not output.

sed -n '20 ,+10p'123.txt prints lines 20 to 30 in 123.txt

sed -n '20 ,$p'123.txt print the 20th to last line in 123.txt

sed -n '2~2p'123.txt print 2,4,6...even lines in 123.txt

sed -n '1~2p'123.txt prints the 1st, 3rd, 5th...odd lines in 123.txt

sed -n 'p'123.txt will repeat the output of the content in 123.txt. sed find output once, p output once.

sed -n '/xxw/p'123.txt will output all lines matching xxw, note: xxw does not contain quotation marks. In sed, to use regular expressions, surround the regular expression with //.

sed -n '/xxw/, /ting/p'123.txt prints the line between the line matching xxw and the line matching ting in 123.txt

sed -n '10, /xxw/p'123.txt performs p operation between the 10th line in 123.txt and the line matching xxw

sed '2c xiaodd'123.txt Replace the second line in 123.txt with xiaodd and display it.

sed '3s#xxw#ting#g' 123.txt Match xxw on line 3 in 123.txt and replace it with ting

echo xiao xian wen xxw is a man | sed 's#^.*en\([az].*\) is .*$#\1#g' Take out xxw (group replacement).

sed -r 's#(.*)#&xxw#g' 123.txt Add xxw to each line in 123.txt, -r means to use regular expansion expressions, $ means everything matched before, not brackets The content in , if necessary, followed by \1 \2 etc.

ls *.txt | sed -r 's#(.*).txt#mv & \1.sh#e ' List all txt files in this directory and pass them to sed, sed matches and replaces "mv 1.txt" 1.sh "e is used to pass it to bash for execution. (Modify all .txt files in the directory to .sh files, and another method is to use renname, for example: rename .txt .sh *.txt

Note: After using -r in sed, the following matching brackets do not need to be escaped \ .

sed -n '2p;4p;5p' 123.txt shows lines 2,4,5 in 123.txt

sed -n '20 d'123.txt delete line 20 in 123.txt

sed -n '20 ,30d'123.txt delete lines 20 to 30 in 123.txt

sed -n '/xxw/d'123.txt delete the line matching xxw in 123.txt

sed '/xxw/a\is'123.txt Add a line of is after the line matching xxw in 123.txt, and then output. Note: The content of 123.txt is not changed here, only the output is changed. Without -n, because with it, only one additional line will be output.

sed '/xxw/i\is'123.txt Add a line of is in front of the line matching xxw in 123.txt, and then output.

sed 's/xxw/ting/'123.txt matches xxw in 123.txt and replaces the first found with ting.

sed 's/xxw/ting/g'123.txt matches xxw in 123.txt and replaces all found with ting.

sed -i 's/25/xxw/' 123.txt Replace the matched string 25 in 123.txt with xxw and save it to the original file. Equivalent to: echo $(sed 's/25/xxw/' 123.txt) > 123.txt

sed 's/xxw/ting/i'123.txt matches xxw in 123.txt and replaces the first found with ting, ignoring case

sed 's/xxw/ting/ig'123.txt matches xxw in 123.txt and replaces all found with ting. ignore case

sed -n '/^\//p' 123.txt matches lines starting with / in 123.txt


Note: sed is not the same as grep, its exit status is 0 whether it finds the specified pattern or not. The exit status of sed is not 0 only if the command has a syntax error. So it is impossible to use [ $? -eq 0 ] in the script to confirm whether the previous sed statement is executed incorrectly.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325989060&siteId=291194637