The use of SED

Source: Internet
Author: User
Tags get ip

SED Overview:

Sed is a stream editor, which is a very important tool in text processing and can be used perfectly with regular expressions. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output. SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program and so on.

Use format: [[email protected] ~]# sed [-NEFR] [action] sed [option] ' {command} ' DataFile

The usual SED option

-e script specifies sed edit command

-F ScriptFile The specified file is the sed edit command

-n silent mode suppresses redundant output information from the execution of SED commands, such as displaying only those rows that have been changed.

-i[suffix],–in-place[=suffix] Replacing and backing up source files

Parameter p: Print matching line

# sed-n ' 2p '/etc/passwd print out line 2nd

# sed-n ' 1,3p '/etc/passwd print out lines 1th to 3rd

# sed-n ' $p '/etc/passwd print out the last line

# sed-n '/user/p '/etc/passwd print out the line containing the user

# sed-n '/\$/p '/etc/passwd print out a line containing $ metacharacters, $ meaning the last line

# sed-n ' $= ' ok.txt print Total rows


Parameters A and I: inserting text and additional text (inserting new lines)


# sed-n '/ftp/p '/etc/passwd print out lines with FTP

# sed '/ftp/a\ 456′/etc/passwd inserts a new row after the line containing the FTP with the content of 456

# sed '/ftp/i\ 123′/etc/passwd inserts a row in front of the line containing the FTP with the contents of 123

# sed '/ftp/i\ ' 123″ '/etc/passwd a new row in front of the line containing the FTP, with the content "123″

# sed ' 5 a\ 123′/etc/passwd insert a new line after line 5th, content 123

# sed ' 5 i\ ' 12345″ '/etc/passwd insert a new line before line 5th, with the content "123 expression 45″


sed -Add embed code after search results


#!/bin/bash

#export LANG=ZH_CN

Find./-name "*.html"-exec grep 10000008/100 ' {} '; -exec sed-i '/10000008/a\<\!--\# include file=\ "\/code.html\"--\> ' {} \;

#

3. Parameter d: Remove the text delete space


Sed-i ' s/[]*//g ' ~/$cid. txt

Delete empty rows without content


Sed '/^$/d ' file.conf > file.new.conf

Sed-i '/^$/d ' ~/$cid. txt

Delete the first line


Sed-i ' 1d ' ~/$cid. txt

Delete the first two lines


Sed-i ' 1,2d ' ~/$cid. txt

del Multiple lines


Cat SCTP.log |sed ' 1d;2d; $d '

Delete the last line:


Sed-e ' $d ' file

Delete the last two lines:


Sed ' n;$! P;d ' File

Sed ' n;$! p;$! D; $d ' file

Delete the last n rows:


$vi sed.sh

$!/bin/bash

a=$ (sed-n ' $= ' file)

Sed $ (($A-n+1)), ${a}d file


$ sed '/user/d '/etc/passwd

Delete a row with string (Del include Love Row head)


Sed-i '/^love/d ' file

Sed-i '/love/d ' file

VI Delete contains strings first 4 lines, after 34 rows


:/strings/-4,+34d

Delete the # # comment line in the configuration file


Sed ' s#\#.*## ' file.conf > file.new.conf

Sed ' s,\#.*,, '

Delete//Number comment lines in the configuration file


Sed ' s#//.*## ' file.conf > file.new.conf

Sed ' [Email protected]//.*@@ '

Delete blank lines by Space and tab


Sed '/^[[:space:]]*$/d ' file.conf > file.new.conf

Sed '/^[[:space:]]*$/d '

Remove spaces from trailing lines


Sed-e ' s/.$//' File > file.new.conf

Add 1 spaces at the end of the line


Sed ' s/[0-9]$/&/g ' file > flile.new.conf

4. Parameter s: replace text, replace command replaces specified pattern with replace mode, in the form:

[A D D r e s S [, address]] S/pattern-to-find/replacement-pattern/[g p w N]


$ sed ' s/user/user/'/etc/passwd replaces the 1th user with User,g to indicate a global substitution

$ sed ' s/user/user/g '/etc/passwd to replace all user with user

$ sed ' s/user/#user/'/etc/passwd replaces 1th user with #user, as for shielding action

$ sed ' s/user//'/etc/passwd replaces the 1th user with an empty

$ sed ' s/user/&11111111111111/'/etc/passwd if you want to attach or modify a long string, you can use the (&) command,& command to save the discovery mode to recall it, and then put it in the replacement string, Here is put & put in front

$ sed ' s/user/11111111111111&/'/etc/passwd here is to put & back

On any line containing the string test, replace 111 with 222 $ sed '/test/s/111/222/g ' sample.txt


5. Quick one-line regular expression command

Here are some line command sets. ([] denotes a space, [TAB] means t a B key)


' S/\. $//g ' Delete end line with a period

'-e/abcd/d ' delete the row containing a B c D

' s/[] [] [] */[]/g ' Delete one to the sky, with a space instead

' s/^ [] [] *//g ' delete line start space

' S/\. [] [] */[]/g ' Delete period followed by two or more spaces, replaced by a space

'/^ $/d ' Delete empty lines

' s/^.//g ' Delete the first character

' S/col \ (... \)//g ' Delete the following three letters immediately after C O L

' s/^ \///g ' removes the first \ from a path

' s/[]/[tab]//g ' removes all spaces and replaces them with the T-a B key

' s/^ [tab]//g ' deletes all t a B keys at the beginning of the line

' S/[tab] *//g ' Delete all t a b keys

6. Use SED to delete the line breaks in the file, kill the chicken which need sledgehammer?

Tr-d ' \ n ' file

I really need sed.


Sed-nr ' H;

$ {

X

s/\n//g;

P

}’

7. Sed to get IP

[[email protected] ~]# ifconfig eth0|sed-ne ' s/^.*addr:\ ([0-9.] *\). *$/\1/p '

192.168.100.180


[Email protected] ~]# ifconfig eth0 | Sed-e '/inet/!d '-e ' s/.*addr://'-e ' s/[].*//'

192.168.100.180

8, \/escape character

Echo/usr/local/bin | Sed ' s/\/usr\/local\/bin/\/common\/bin/'

Find. -name "*.html" |xargs sed-i ' s#/canapp/evaluation/html/index.html#http://www.wallcopper.com/eva/#g '

You can replace the tab in the. c. h file in the current directory with 4 spaces.

Find. -name "*. [C|h] "| Xargs sed-i ' s/\t//g '

9. Parameter f:

The following three commands are equal, replace 111 with 222 on any line containing the string test, and then replace the character F with the Hello


$ sed '/test/s/111/222/g; S/f/hello/g ' Sample.txt

$ Sed-e '/test/s/111/222/g '-e ' s/f/hello/g ' sample.txt

Option-F: will be executed so the compile command is placed in the file


$ more Scher

/test/s/111/222/g

S/f/hello/g ' Sample.txt

$ sed-f Scher sample.txt

10. Parameter Q: Indicates jumping off sed. Mates with up to one address parameter.

Prints a line that contains a or b character, and stops printing as soon as it encounters a number.


$ sed-n-E '/[0-9]/q '-e '/[a b]/p '

To China quit

Sed ' china/q ' myfile.txt

11, parameter-i suffix: replace and change the source file backup suffix

$ Sed-i.bak ' s/123/efg/g ' a.txt backup source file

12. Parameter r: Insert file in a line

$ Sed-i ' 2 R readfile.txt ' Writefile.txt

13. Parameter W: The contents of the read-in file are deposited into another file (Temp.txt). Mates with up to one address parameter.

Copy the data row containing the test string in the Sample.txt file to the Temp.txt archive (no content in the original temp.txt)


$ Sed-e '/test/w temp.txt ' sample.txt

14. Parameter Y: Converts the characters in the data. Mates with up to two address parameters.

1. Replace the lowercase letter in the file with uppercase letters.


$ Sed-e ' y/abcdefghijklmnopqrstuvwxyz/abcdefghijklmnopqrstuvwxyz/' sample.txt

The number of characters before and after must be the same.


15, Parameters!: Indicates that the function parameter is not executed.

The file is removed except for the important string, and all the rest.


$ Sed-e '/important/!d ' sample.txt

16, Parameter =: Indicates the number of lines printed data. Mates with up to two address parameters.

1. Print out the file and display the number of lines.


$ Sed-e ' = ' sample.txt

Calculate Total rows


$ Sed-n ' $= ' a.txt


This article is from "Jet ' aime" blog, declined reprint!

The use of SED

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.