Linux sed command: stream editor for filtering and converting text
Linux sed command Function Description
The sed command can be used to filter and convert text.
sed is a stream editor, which is a very important tool in text processing and works perfectly with regular expressions. When processing, the current line is stored in a temporary buffer called "pattern space", then the contents of the buffer are processed with the sed command, and when processing is complete, the contents of the buffer are sent to the screen. Then process the next line, and so on, until the end of the file. The contents of the file do not change unless you use redirected storage output. sed is mainly used to automatically edit one or more files; to simplify repeated operations on files; to write conversion programs, etc.
Linux sed command Syntax
sed [option] [script] [input file]
The meaning of each option in the command is shown in the following table.
Option | Description |
---|---|
--posix |
Close all GNU extensions |
-n |
Cancel auto print mode space |
-r |
Use extended regular expressions in scripts |
-s |
Treat input files as individual files rather than as one long continuous input |
-u |
Read the least amount of data from the input file and refresh the output more frequently |
-e <Script> |
Adds the specified script to the run list of the program |
-f <Script File> |
Adds the specified script file to the run list of the program |
-c |
Use copy instead of renaming |
-i <Suffix> |
Edit the file in Place |
-l <Length> |
Specifies the expected length of a newline |
The sed command tells sed how to process each input line specified by an address, or all input lines if no address is specified. The following table lists the commonly used commands.
Command | Description |
---|---|
a\ |
Add one or more lines after the current line. In the case of multiple lines, except for the last line, each line should be continued with "\" at the end. |
c\ |
Replace the text in the current line with the new text after this symbol. In the case of multiple lines, you need to use "\" at the end of each line except for the last line. |
i\ |
Insert text before the current line. In the case of multiple lines, except for the last line, each line should be continued with "\" at the end. |
d | Delete rows |
h | Copies the contents of the schema space to the staging buffer |
H | Appends the contents of the schema space to the staging buffer |
g | Copy the contents of the staging buffer to the schema space, overwriting the original contents |
G | Append the contents of the staging buffer to the pattern space, after the original contents |
l | List non-printing characters |
p | Print the line |
n | Read the next input line and start processing it from the next command instead of the first command |
q | End or exit sed |
r | Read input lines from a file |
! | Apply the command to all lines other than the selected line |
s | Replace one string with another |
g | Do global substitution within rows |
w | Writes the selected line to the file |
x | Swap the contents of the staging buffer with the schema space |
y | Replace a character with another character (you cannot use the y command on regular expressions) |
Like grep, sed supports special metacharacters for pattern finding and replacement. The difference is that sed uses regular expressions with patterns enclosed between the slashes "\".
To change the regular expression separator "\" to another character, such as o, just add a backslash before the character, followed by the regular expression, followed by the character. For example: sed -n ‘\o^Myop’ datafile.
The following table lists the commonly used regular expression meta characters.
Meta Characters | Function | Example |
---|---|---|
^ |
Line head locator | /^my/ matches all lines starting with my |
$ |
End-of-line locator | /my$/ matches all lines ending with my |
. |
Matches a single character except a newline character | /m..y/ matches a line containing the letter m, followed by two arbitrary characters, followed by the letter y |
* |
Matches zero or more leading characters | /my*/ matches lines containing the letter m followed by zero or more Y letters |
[] |
Matches any character in the specified character group | /[Mm]y/ Matches rows containing My or my |
[^] |
Match any character that is not in the specified character group | /[^Mm]y/ matches a line containing y, but the character before y is not M or m |
\(..\) |
Save the matched character | 1,20s/\(you\)self/\1r/ marks the pattern between the meta-characters and saves it as tag 1, which can be referenced later using V1. Up to 9 tags can be defined, numbered from the left, with the leftmost being the first. In this example, lines 1 to 20 are processed, you is saved as tag 1, and if youself is found, it is replaced with your |
& |
Save the lookup string for reference in the replacement string | s/my/**&**/ The symbol & represents the lookup string. my will be replaced with **my** |
\< |
Word head locator | /\<my/ matches lines containing words starting with my |
\> |
Suffix locator | /my\>/ matches lines containing words ending in my |
x\{m\} |
M x’s in a row | /9\{5\}/ Matches a line containing 5 consecutive 9’s |
x\{m,\} |
At least m x’s | /9\{5,\}/ Matches a line containing at least 5 consecutive 9s |
x\{m,n\} |
At least m, but not more than n x’s | /9\{5,7\}/ Matches lines containing 5 to 7 consecutive 9s |
Linux sed command Example
Show only line 3 of the /etc/passwd file
sed -n '3p' /etc/passwd
Output:
Display only lines 10 to 20 of the /etc/passwd file
[root@rhel ~]# sed -n '10,20p' /etc/passwd
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologin
pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologin
Exclude lines 2 through 5 to show the contents of the /etc/passwd file
[root@rhel ~]# sed '2,5d' /etc/passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
........................(Omitted)