Linux sed command detailed __linux

Source: Internet
Author: User

Sed is a good file processing tool, itself is a pipe command, mainly in the behavior unit, you can replace, delete, add, select and other specific work, the following first understand the use of SED
The SED command line format is:
sed [-nefri] ' command ' Enter text

Common options:
-N: Use Quiet (silent) mode. In general sed usage, all data from stdin is generally listed on the screen. However, if you add the-n parameter, only the line (or action) that is specifically handled by SED will be listed.
-E: SED action edit directly on instruction column mode;
-F: Directly write the action of SED in a file, the-f filename can perform the SED action in filename;
-r:sed's actions support the syntax of extended formal notation. (Presupposition is the basic formal representation of French law)
-I: Directly modify the contents of the file read, not by the screen output.

Common commands:
        a  : New, A can be followed by strings, and these strings will appear on the new line (current next line) ~
        c  : Replace, C can be followed by strings, these strings can replace n1,n2 between the lines.
        d  : Delete, because it is deleted ah, so d usually do not pick up any of the drums;
          i  : Insert, I can be followed by strings, and these strings will appear on a new line (currently the previous line);
          p  : Print, that is, to print out a selection of information. Normally p will work with the parameter Sed-n ~
         s  : Replace, can be directly replaced by the working Mile. Usually this s action can be paired with the formal notation. For example, 1,20s/old/new/g is.

Example: (assuming we have a file named ab)
      Delete a line
     [root@localhost Ruby]  # sed ' 1d ' ab              #删除第一行  
     [root@localhost ruby] # sed ' $d ' ab                #删除最后一行
     [root@localhost ruby] # sed ' 1,2d ' ab             #删除第一行到第二行
     [ Root@localhost ruby] # sed ' 2, $d ' ab            #删除第二行到最后一行

Show a row
. [Root@localhost Ruby] # sed-n ' 1p ' ab #显示第一行
[Root@localhost Ruby] # sed-n ' $p ' ab #显示最后一行
[Root@localhost Ruby] # sed-n ' 1,2p ' ab #显示第一行到第二行
[Root@localhost Ruby] # sed-n ' 2, $p ' AB #显示第二行到最后一行

Using patterns for querying
[Root@localhost Ruby] # sed-n '/ruby/p ' ab #查询包括关键字ruby所在所有行
[Root@localhost Ruby] # sed-n '/\$/p ' AB #查询包括关键字 $ All rows, use backslash \ 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 #第一行后增加字符串 "Drink Tea"
Hello!
Drink tea
Ruby is me,welcome to my blog.
End
[Root@localhost Ruby] # sed ' 1,3a drink tea ' ab #第一行到第三行后增加字符串 ' drink tea '
Hello!
Drink tea
Ruby is me,welcome to my blog.
Drink tea
End
Drink tea
[Root@localhost Ruby] # sed ' 1a drink tea\nor coffee ' ab #第一行后增加多行, using line breaks \ n
Hello!
Drink tea
or coffee
Ruby is me,welcome to my blog.
End

Replace one or more lines
[Root@localhost Ruby] # sed ' 1c Hi ' AB #第一行代替为Hi
Hi
Ruby is me,welcome to my blog.
End
[Root@localhost Ruby] # sed ' 1,2c Hi ' ab #第一行到第二行代替为Hi
Hi
End

Replace a part of a row
Format: sed ' s/string to replace/new string/g ' (string to be replaced can be in regular expression)
[Root@localhost Ruby] # sed-n '/ruby/p ' ab | Sed ' s/ruby/bird/g ' #替换ruby为bird
[Root@localhost Ruby] # sed-n '/ruby/p ' ab | Sed ' s/ruby//g ' #删除ruby

Insert
[Root@localhost Ruby] # sed-i ' $a bye ' ab #在文件ab中最后一行直接输入 ' bye '
[Root@localhost ruby]# Cat AB
Hello!
Ruby is me,welcome to my blog.
End

Use SED to delete a line of character openings, and if you are sure that there are only spaces at the beginning, use this command:

Sed ' s/^ *//' infile

If you are not sure whether it is a space or a tab, use this command:

Sed ' s/^[[:space:]]*//' infile

Delete a space at the end of a line of characters with SED:

Sed-e ' s/[]*$//g

SED TR replaces multiple spaces with a single space

sed ' s/[] []*//g '
If the Space and tab coexist, use the
sed-e ' s/[[:space:]][[:space:]]*//g ' filename

Replace a space in a document name
newfile=${oldfile///_}
with tr:
Find. -type f-name "* *"-print |
while read name; Todo
na=$ (echo $name | tr ' _ ')
if [[$name!= $na]]; Then
MV "$name" $na
Fi
Done
Modify IFS
#!/bin/sh
ifs=@ Read name Address
echo "A mail to $name at $address"
Read subject
echo "Subject: $subject"
Or
#!/bin/sh
Ifs=:
For P in $PATH
Todo
If [-X $p/$1]
Then
Echo $p/$1
Return
Fi
Done
echo "No in Your Path" 1 > &2
Return 1
Or
(ifs=:; for D in $PATH; doing for F in $D/*gif*; does [-X $F] && echo $F; done)
Today, a shell program, the results of the transfer of the file name some have a space, resulting in cannot be executed, can not find the original file, anxious to solve the file name of the blanks are all replaced with the underline, in fact, should be able to solve the program, continue to see what methods
The following is a script to find a replacement space file on the Internet
with tr:
Find. -type f-name "* *"-print |
while read name; Todo
na=$ (echo $name | tr ' _ ')

if [[$name!= $na]]; Then
MV "$name" $na
Fi
Done

Sed at the beginning of the line, the end of lines append do not wrap.

Sed ' s/^/append_helloworld/g ' yourfile
Sed ' s/$/insert_helloworld/g ' yourfile The following is a newline: Append command
Syntax format:
[Line-address]a/text
such as sed ' 10A/ABCD ' sed.txt appends a line of ABCD characters to line 10th of the Sed.txt file.
Sed '/UNIX/A/ABCD//NDCBA ' sed.txt append one line of ABCD/NDCBA characters to all occurrences of the UNIX character in the Sed.txt file
Sed '/UNIX/A/ABCD/N/DCBA ' sed.txt appends two lines of characters to all occurrences of the UNIX character in the Sed.txt file, where the first behavior is the ABCD second behavior DCBA. Insert command
Syntax format:
[Line-address]i/text
such as sed '/UNIX/I/ADFLAJFLAD/N/ADFADFAJDLF ' sed-s.txt appends two lines of characters to all occurrences of the UNIX character in the Sed.txt file, where the first behavior is the ABCD second behavior DCBA.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.