Linux sed command usage
- 2020-04-02 01:48:46
- OfStack
The sed command line format is:
Sed [-nefri] 'command' enters the text
Common options:
-n: use silent mode. In normal sed usage, all information from STDIN is typically listed on the screen. But if you add the -n parameter, only the line (or action) that has been specially processed by sed will be listed.
-e: direct sed action editing on command line mode;
-f: directly write the sed action in a file, -f filename can perform the sed action in filename;
The action of -r: sed supports the syntax of extended normal notation. (default is basic normal notation syntax)
- I: directly modify the contents of the read file, rather than output from the screen.
Common commands:
a.
: new, a can be followed by a string, and these strings will appear in a new line (the current next line) ~
c
: replace, c can be followed by a string, these strings can replace the line between n1,n2!
d
: delete, because it is to delete, so d usually do not pick up any dongdong;
i.
: insert, I can be followed by a string, and these strings will appear on a new line (the current previous line);
p
: print, or print the selected data. P normally works with the parameter sed -n ~
s.
: substitute, can carry on the work of substitute directly! Usually this s action can be accompanied by a normal notation! For example, 1,20s/old/new/g will do!
Example :(suppose we have a file named ab)
To delete a line
[root@localhost ruby] # sed '1d' ab # removes the first line
[root@localhost ruby] # sed '$d 'ab # removes the last line
[root@localhost ruby] # sed '1,2d 'ab
Delete the first to second lines
[root@localhost ruby] # sed '2,$d' ab
Delete the second to last line
According to a line
.
[root@localhost ruby] # sed -n '1p' ab
# shows the first line
[root@localhost ruby] # sed -n '$p' ab
# shows the last line
[root@localhost ruby] # sed-n '1,2p' a # shows the first to second lines
[root@localhost ruby] # sed-n '2,$p' a # shows the second to last line
Use patterns for queries
[root@localhost ruby] # sed-n '/ruby/p' ab
The # query includes all rows where the keyword ruby resides
[root@localhost ruby] # sed-n '/\$/p' a # query includes all lines where the keyword $is located, using backslash \ to mask special meanings
Adds one or more lines to the string
[root @ localhost ruby] # cat ab
Hello!
Ruby is me,welcome to my blog.
The end
[root@localhost ruby] # sed '1a drink tea' ab
Add the string "drink tea" after the first line
Hello!
Ultimately responds tea
Ruby is me,welcome to my blog.
The end
[root@localhost ruby] # sed '1,3a drink tea' ab # add the string "drink tea" after lines 1 through 3
Hello!
Ultimately responds tea
Ruby is me,welcome to my blog.
Ultimately responds tea
The end
Ultimately responds tea
[root@localhost ruby] # sed '1a drink tea\nor coffee' ab
Add multiple lines after the first line in #, using the newline character \n
Hello!
Ultimately responds tea
Or coffee
Ruby is me,welcome to my blog.
The end
Replace one or more lines
The first line of [root@localhost ruby] # sed '1c Hi 'a # is replaced by Hi
Hi
Ruby is me,welcome to my blog.
The end
[root@localhost ruby] # sed '1,2c Hi 'a # first to second lines instead of Hi
Hi
The end
Replace a part of a row
Format: sed 's/ string to be replaced/new string /g'
(the string to be replaced can be replaced with a regular expression.)
[root@localhost ruby] # sed-n '/ruby/p' ab | sed 's/ruby/bird/g'
Replace ruby with bird
[root@localhost ruby] # sed-n '/ruby/p' ab | sed 's/ruby//g'# remove ruby
insert
[root@localhost ruby] # sed - I '$a bye' ab # enter "bye" directly on the last line of the file ab
[root @ localhost ruby] # cat ab
Hello!
Ruby is me,welcome to my blog.
The end
bye