sed of linux command

Sed is a pipeline command that can analyze standard input, and sed can also replace, delete, add, select specific lines and other functions of data.

usage:

[root@www ~]# sed [-nefr] [action]

parameter:

-n: Use quiet mode. In normal sed usage, all data from STDIN would normally be listed on the screen. But if the -n parameter is added, only the line that has been specially processed by sed will be listed;

-e: sed action editing directly on the command line mode;

-f: Write the sed action directly in a file, -f filename can execute the sed action in filename

-r: The action of sed supports the syntax of extended regular expressions.

-i: Modify the contents of the read file directly instead of outputting it to the screen.

Action description: [n1[,n2]]function

n1, n2: may not exist, generally represents the number of rows selected to perform the action; for example, if my action needs to be performed between 10 and 20 rows, then '10,20[action behavior]'

function has the following parameters:

a: new, a can be followed by strings, and these strings will appear on a new line;

c: Replace, c can be followed by strings, and these strings can replace the lines between n1 and n2!

d: delete, because it is a deletion, there is usually no parameter after d;

i: Insert, i can be followed by strings, and these strings will appear on a new line;

p: print, that is, print out a selected data. Usually p is run with the parameter sed -n

s: Replace, you can directly perform the replacement work.

Add/Remove functionality by row

Example 1: List the first ten lines of /etc/passwd and print the line number, at the same time, please delete lines 2~5

[root@www ~]# nl /etc/passwd | head -n 10 | tail -n 10 | sed '2,5d'

Specific content omitted.

Example 2: Following the example above, add the word "drink tea" after the second line

[root@www ~]# nl /etc/passwd | head -n 10 | tail -n 10 | sed '2a drink tea'

Specific content omitted

Replacing and Displaying Functions in Lines

Example 3: Replace the second to fifth lines with "No 2-5 number"

[root@www ~]# nl /etc/passwd | head -n 10 | tail -n 10 | sed '2,5c No 2-5 number'

Specific content omitted

Example 4: List only lines 5-7 in the /etc/passwd file (we have always done this before, this is another method)

[root@www ~]# nl /etc/passwd | sed -n '5,7p'

Specific content omitted

Find and replace function of part of data

In addition to the processing mode of the entire line, sed can also use the behavior unit to perform partial data search and replace functions. Basically sed's find and replace is pretty similar to vi, it's a bit like this:

sed 's/ string to be replaced/new string /g'

The part of the special font above is a keyword, please write it down!

example:

Use /sbin/ifconfig to query the IP, and then use the keyword grep to select a key line of data

[root@www ~]#  /sbin/ifconfig  | grep 'inet addr'

        inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

Delete the part before the IP

[root@www ~]# /sbin/ifconfig | grep 'inet addr' | sed 's/^.*addr://g'

192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

Compared with the above, the front part is less

Modify the content of the file directly

sed can even directly modify the contents of a file without having to use piped commands or data stream redirection. However, this command modifies the file directly, so don't just experiment with system files. Implemented by the -i parameter, the following content is the same as the content of the modified part.

Guess you like

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