Append command (Command a)
Sed ' [address] a the-line-to-append ' input-file
Append a row after the second line (there may be a problem with the original, there is no line number to write the name)
[[Email protected] ~]# sed ' 2 a 203,jack johnson,engineer ' Employee.txt
101,johnnynynyny Doe,ceo
102,jason Smith,it Manager
203,jack Johnson,engineer
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales Manager
#106, Jane miller,sales Manager
#107, Jane miller,sales Manager
Append a line to the end of the Employee.txt file:
[[Email protected] ~]# sed ' $ A 106,jack johnny,engineer ' Employee.txt
101,johnnynynyny Doe,ceo
102,jason Smith,it Manager
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales Manager
#106, Jane miller,sales Manager
#107, Jane miller,sales Manager
106,jack Johnny,engineer
Append two lines after the line that matches Jason
[[Email protected] ~]# sed '/jason/a\
> 203,jack johnson,engineer\
> 204,mark smith,slaes Engineer ' Employee.txt
101,johnnynynyny Doe,ceo
102,jason Smith,it Manager
203,jack Johnson,engineer
204,mark Smith,slaes Engineer
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales Manager
#106, Jane miller,sales Manager
#107, Jane miller,sales Manager
You can use \ n to wrap between multiple rows, so that you do not have to break the line, the above command is equivalent to:
[[Email protected] ~]# sed '/jason/a 203,jack johnson,engineer\n204,mark smith,slaes Engineer ' Employee.txt
101,johnnynynyny Doe,ceo
102,jason Smith,it Manager
203,jack Johnson,engineer
204,mark Smith,slaes Engineer
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales Manager
#106, Jane miller,sales Manager
#107, Jane miller,sales Manager
Modify command (Command C)
Sed ' [address] c The-line-to-insert ' Input-file
Replace line 2nd with new data
[[Email protected] ~]# sed ' 2 c 202,jack,johnson,engineer ' Employee1.txt
101,john Doe,ceo
202,jack,johnson,engineer
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales Manager
#106, Jane miller,sales Manager
#107, Jane miller,sales Manager
Here the command C is equivalent to the replacement: sed ' 2s/.*/202,,jack,johnson,engineer/' employee.txt
SED can also use multiple lines to replace a row
Replace rows that match Raj with two rows of data
[[Email protected] ~]# sed '/raj/c \
> 203,jack,johnson,engineer \
> 204,mark smith,slaes Engineer ' Employee.txt
101,johnnynynyny Doe,ceo
102,jason Smith,it Manager
203,jack,johnson,engineer
204,mark Smith,slaes Engineer
104,anand Ram,developer
105,jane Miller,sales Manager
#106, Jane miller,sales Manager
#107, Jane miller,sales Manager
Commands A, I, and C are used together
L a add "Jack Johnson" behind "Jason "
L I insert "Mark Smith" in front of "Jason"
L C Replace "Jason" with "Joe Mason "
[[Email protected] ~]# sed '/jason/{
A\
204,jack Johnson,engineer
I\
202,mark Smith,slaes Engineer
C\
203,joe Mason,sysadmin
} ' Employee.txt
101,johnnynynyny Doe,ceo
202,mark Smith,slaes Engineer
203,joe Mason,sysadmin
204,jack Johnson,engineer
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales Manager
#106, Jane miller,sales Manager
#107, Jane miller,sales Manager
Print invisible characters (command L)
First build the test file
[Email protected] ~]# vim Tab.txt
FName First Name
LName Last Name
Mname Middle Name
[Email protected] ~]# sed-n ' l ' tab.txt
FName First name$
LName Last name$
Mname Middle name$
$
If a number is specified after L, a non-visible auto-wrap is used at the nth character
[Email protected] ~]# sed-n ' l ' employee.txt
101,johnnynynyny do\
e,ceo$
102,jason smith,it \
manager$
103,raj reddy,sysad\
min$
104,anand ram,devel\
oper$
105,jane miller,sal\
Es manager$
#106, Jane miller,sa\.
Les manager$
#107, Jane miller,sa\.
Les manager$
This feature is only available for GNU SED
Print line number (Command =)
Print line numbers for all lines
[[Email protected] ~]# sed ' = ' employee.txt
1
101,johnnynynyny Doe,ceo
2
102,jason Smith,it Manager
3
103,raj Reddy,sysadmin
4
104,anand Ram,developer
5
105,jane Miller,sales Manager
6
#106, Jane miller,sales Manager
7
#107, Jane miller,sales Manager
Tip: Use command = and command n to display line numbers and content on the same line
Print line numbers for only one-line lines
[[Email protected] ~]# sed ' 1,3= ' employee.txt
1
101,johnnynynyny Doe,ceo
2
102,jason Smith,it Manager
3
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales Manager
#106, Jane miller,sales Manager
#107, Jane miller,sales Manager
Print the line that contains the keyword "Jane" while printing the contents of the input file:
[[Email protected] ~]# sed '/jane/= ' employee.txt
101,johnnynynyny Doe,ceo
102,jason Smith,it Manager
103,raj Reddy,sysadmin
104,anand Ram,developer
5
105,jane Miller,sales Manager
6
#106, Jane miller,sales Manager
7
#107, Jane miller,sales Manager
If you want to display only the line number but not the contents of the row, use the-N option to match the command =:
[Email protected] ~]# sed-n '/raj/= ' employee.txt
3
Total rows of printed files
[Email protected] ~]# sed-n ' $= ' employee.txt
7
Convert character (command y)
Command y converts characters based on their corresponding positions, one of the benefits of converting uppercase letters to lowercase and vice versa
The following example
[[Email protected] ~]# sed ' y/abcde/abcde/' employee.txt
101,johnnynynyny Doe,ceo
102,jason Smith,it MAnAgEr
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales MAnAgEr
#106, JAnE miller,sales MAnAgEr
#107, JAnE miller,sales ManAgEr
Convert all lowercase letters to uppercase
[[Email protected] ~]# sed ' y/abcdefghijklmnopqrstuvwxyz/abcdefghijklmnopqrstuvwxyz/' employee.txt
101,johnnynynyny Doe,ceo
102,jason Smith,it MANAGER
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales MANAGER
#106, JANE miller,sales MANAGER
#107, JANE miller,sales MANAGER
Manipulating Multiple Files
Search for root in/etc/passwd and print it out
[Email protected] ~]# sed-n '/root/p '/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
Search for root in/etc/group and print it out
[Email protected] ~]# sed-n '/root/p '/etc/group
root:x:0:
Search for root in both/etc/passwd and/etc/group
[Email protected] ~]# sed-n '/root/p '/etc/passwd/etc/group
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:
Exit sed (Command q)
When SED encounters the command q, it exits immediately, and subsequent commands in the current loop are not executed and will not continue to loop
Exit after printing the first line
[[Email protected] ~]# sed ' 1 q ' employee.txt
101,johnnynynyny Doe,ceo
Exit after printing 5 lines
[[Email protected] ~]# sed ' 5 q ' employee.txt
101,johnnynynyny Doe,ceo
102,jason Smith,it Manager
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales Manager
Print all lines until you encounter the line that contains the keyword manager
[[Email protected] ~]# sed '/manager/q ' employee.txt
101,johnnynynyny Doe,ceo
102,jason Smith,it Manager
Note: The Q command cannot specify an address range (or pattern range) and can only be used with a single file (or a single mode)
Reading data from a file (command R)
[[Email protected] ~]# sed ' $ r log.txt ' Employee.txt
101,johnnynynyny Doe,ceo
102,jason Smith,it Manager
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales Manager
#106, Jane miller,sales Manager
#107, Jane miller,sales Manager
Log:input.txt
Log
Log:testing resumed
Log
Log:output created
[Email protected] ~]# sed '/raj/r. /sed8awk/log.txt ' Employee.txt
101,johnnynynyny Doe,ceo
102,jason Smith,it Manager
103,raj Reddy,sysadmin
104,anand Ram,developer
105,jane Miller,sales Manager
#106, Jane miller,sales Manager
#107, Jane miller,sales Manager
Sed additional commands