Linux sed command is mainly used for text processing, can handle the lines and columns of text, the following small series will give you a detailed introduction to the usage of the sed command in Linux, interested friends may wish Let's understand it.
sed command format is:
sed [-nefri] & lsquo; command & rsquo; input text
common options:
-n: Use the silent mode. In the general sed usage, all data from STDIN will generally be listed on the screen. However, if the -n parameter is added, only the line (or action) that has been specially processed by sed will be listed.
-e: Perform sed action editing directly in command line mode;
-f: directly write sed action in a file, -f filename can execute inside filename Sed action;
The action of -r:sed supports the syntax of extended regular notation. (The default is the basic regular notation syntax)
-i: Directly modify the contents of the read file instead of being output by the screen.
Commonly used commands:
a : New, a can be followed by a string, and these strings will appear on a new line (current next line) ~
c : Substitute, c can be followed by a string, these strings can replace the line between n1, n2!
d: delete, because it is deleted, so d usually does not pick up any 咚咚;
i: insert, i can be followed by a string, and these strings will be new The line appears (currently the previous line);
p: prints, and also prints a selected item. Usually p will work with the parameter sed -n~
s : Replace, you can directly replace the work! Usually this s action can be combined with regular notation! For example, 1,20s/old/new/g is it!
Example: (assuming we have a file named ab)
Delete a line
[root@localhost ruby] # sed ‘1d’ ab # Delete the first line
[root@localhost ruby] # sed ‘$d’ ab # Delete the last line
[root@localhost ruby] # sed ‘1,2d’ ab # Delete the first line Go to the second line
[root@localhost ruby] # sed ‘2,$d’ ab # Delete the second line to the last line
Display a line
. [root@localhost ruby] # sed -n ‘1p’ ab #Show the first line
[root@localhost ruby] # sed -n ‘$p’ ab #Show the last line
[root@localhost ruby] # sed -n ‘1,2p’ ab #Show the first line to the second line
[root@localhost ruby] # sed -n ‘2,$p&rsquo ; ab #Show the second line to the last line
Use the mode to query
[root@localhost ruby] # sed -n ‘/ruby/p’ ab #Query including the keyword ruby All lines
[root@localhost ruby] # sed -n ‘/\\$/p’ ab #Query all lines including the keyword $, use backslashes\\mask special meaning
Add one or more lines of string
[root@localhost ruby]# cat ab
Hello!
ruby is me,welcome to my blog.
end
[root@localhost ruby] # sed ‘1a drink tea’ ab #Add characters after the first line String “drink tea”
Hello!
drink tea
ruby is me,welcome to my blog.
end
[root@localhost ruby] # sed ‘1,3a drink Tea’ ab #Add the string after the first line to the third line“drink tea”
Hello!
drink tea
ruby is me,welcome to my blog.
drink tea
end
drink tea
[root@localhost ruby] # sed ‘1a drink tea\ or coffee’ ab #Add multiple lines after the first line, use line breaks \
Hello!
drink tea
or coffee
ruby is me,welcome to my blog.
end
instead of one or more lines< Br>
[root@localhost ruby] # sed ‘1c Hi’ ab #The first line is replaced by Hi
Hi
ruby is me,welcome to my blog.
end
[root@localhost ruby] # sed ‘1,2c Hi’ ab #The first line to the second line instead of Hi
Hi
end
Replace a part of a line
Format: sed & lsquo;s/string to be replaced /new string /g’ (The string to be replaced can be expressed by regular expression式)
[root@localhost ruby] # sed -n ‘/ruby/p’ ab |
Sed ‘s/ruby/bird/g’ #replace ruby as bird
[root@localhost ruby] # sed -n ‘/ruby/p’ ab |
Sed ‘s/ruby//g’ #去除ruby
Insert
[root@localhost ruby] # sed -i ‘$a bye’ ab #last line in file ab Directly enter “bye”
[root@localhost ruby]# cat ab
Hello!
ruby is me,welcome to my blog.
end
bye
The above is the usage of the sed command under Linux, the usage of the sed command Similar to awk, but the sed command is more powerful for column processing, and you can update it according to the actual situation.
Linux mail command is often used by system administrators. It is a very practical c
Linux Container referred to as LXC, compared with traditional virtualization technology, it has the
Ubuntu is a kind of Linux system. The biggest feature is that it has a software cen
After installation of CentOS 6.5, many people can not connect to the wireless network, most of the r
What are the ways to clean up Linux disk space?
How to use the test command in Linux
How to detect when Linux system is invaded?
How to identify Linux soft links and hard links
Modify Linux password prompt it is based on a dictionary word What to do?
Linux blocking browser request alert method on browser
Knowledge about file I/O operations under Linux
RedHat how to install log4cxx log library
How does CentOS VPS open port 80
Kali Linux crack wifi password with what software is good?
Win7 can not lock the program to the taskbar solution
Creating Windows Vista System Health Report
Under the Mercury router vista system to view and modify the IP
Windows Vista new file association setting method
Teach you how to bind the MAC address under Vista system
What if your Windows system crashes? 6 strokes to fix Windows
Fast solution for graphics problems
Win10 Consumer Preview Exposure! Win10 consumer preview and technology preview version difference
How does the official version of Win10 log in to your microsoft account? After