[Shell Foundation]--sed Command

Source: Internet
Author: User

About SED

Sed is an online editor that processes a single line of content at a time.
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.

Sed detailed

sed [options] sed edit command input file
Shell Command | sed [options] sed edit command
sed [options]-F SED script file input file

Common sed options
-N Show only rows that match processing
-I changes directly in the file without displaying
-R supports extended regular expressions
-e When performing multiple editing commands (generally used; instead)

Common SED editing commands
P: Print matching line
D: Deletes the specified row delete
A: Append append after matching line
I: inserting insert before matching line
C: Whole line substitution
R: Reads the contents of the file into the read
W: Write text to file write
S: String substitution (matches regular expression) substitution

(1) '//p ' =!

show X to y lines
# sed-n ' X,yp '

x to Y lines do not print
# sed-n ' x,y!p '! Take the reverse

show X to X+y line
# sed-n ' X,+yp '

Show Line 1th
# sed-n ' 1p '

Show last 1 rows
# sed-n ' $p '

show lines that start with 0-9 numbers
# sed-n '/^[0-9]/p '
# sed-n '/Regular Expression/{x,yp} '

output matches a row and displays its line number in the text
# sed-n '/Regular expression/{=;p} '

(2) '//d '
# sed-n ' X,yd '
# sed-n '/Regular expression/{x,yd} '

Delete all blank lines
# sed '/^$/d '

(3) ; or- e
Delete lines beginning with empty lines and # numbers
# sed '/^$/d ; /^#/d'
# sed- e '/^$/d '-e '/^#/d ' Multi-conditional deletion is used '; '


(4) '//a\ ' line appended with '//A ' next line
Add three * * * to the line after Fred begins.
# cat Gongdatext.txt | sed '/^fred/a\*** '

appends a row to the next line of the nth row (note that n cannot be the number of lines where the text does not exist)
# seq ' na 111111111 '

Appends a row to the next line of the matched row (note that another row is appended)
# seq '/Regular expression/a 111111111 '

(5) Append before '/I ' line
Appends a row to the previous line of the nth row
# seq ' ni 111111111 '

appends a row to the last line
# seq ' $i 111111111 '

Append a row to the previous line of the matched row
# seq '/Regular expression/i 111111111 '

(6) '//C ' full line replacement
Replace the contents of the entire row of the nth row with that content
# seq ' nC 111111111 '

Replace the content of the matching row with that content
# seq '/Regular expression/C 111111111 '

(7) ' s/old/new/g ' replacement
also replace # @ with Gongda
# cat Gongda.txt | sed-e ' s/#/gongda/g '-e ' s/@/gongda/g '

Replace the ABCDEFG of 1 and 2 lines at the same time Alvin
# cat Gongda.txt | sed-n ' 1,2s/abcdefg/alvin/p '

(8) -I writes the modification to the original file

(9) R
read the contents of the/etc/hosts file after the end of the Test.txt file
# sed ' $R/etc/hosts ' Test.txt

Read the contents of the/etc/mtab file after/dev/sda1
# df-h | sed '/dev\/sda1/ r/etc/mtab'

Sed exercises

The test text is gongdatext.txt and reads as follows:

1 Steve blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300
2 Betty boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
3 Igor chevsky:385-375-8395:3567 Populus place, Caldwell, NJ 23875:6/18/68:23400
4 Norma corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
5 Jennifer cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900
6 Jon ' s deloach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
7 Karen evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100
8 Karen evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
9 Karen evich:284-758-2867:23 november-december Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
"Fred" Fardbarkle:674-843-1385:20 Parak Lane, Deluth, MN 23850:4/12/23:780900
One Fred fardbarkle:674-843-1385:20 Parak Lane, Deluth, MN 23850:4/12/23:780900
Lori gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Paco gutierrez:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500
Ephram hardy:293-259-5395:235 Carltonlane, Joliet, IL 73858:8/12/20:56700
James ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Barbara kertz:385-573-8326:832 Ponce Drive, Gary, in 83756:12/1/46:268500
Lesley kirstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
William kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Sir lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500
Jesse neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000
Zippy pinhead:834-823-8319:2356 Bizarro Ave., Farmount, IL 84357:1/1/67:89500
Arthur putie:923-835-8745:23 Wimp Lane, Kensington, DL 38758:8/31/69:126000
Popeye sailor:156-454-3322:945 Bluto Street, Anywhere, USA 29358:3/19/35:22350
Jose santiago:385-898-8357:38 Fife, Abilene, TX 39673:1/5/58:95600
Tommy savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200
Yukio takeshida:387-827-1095:13 Uno Lane, Ashville, NC 23556:7/1/29:57000
Vinh tranh:438-910-7449:8235 Maple Street, Wilmington, VM 29085:9/23/63:68900

(1) Temporarily replace Jon's with Jonathan.
# sed "S/jon ' s/jonathan/g" gongdatext.txt

(2) Replace "Fred" with Gongda, which includes the "" number.
# sed ' s/' Fred '/gongda/g ' gongdatext.txt

(3) Delete the first three rows and the 11th row
# Cat Gongdatext.txt | Sed-e ' 1,3d '-e ' 11d '

(4) Display 5-10 rows
# sed-n ' 5,10p ' gongdatext.txt

(5) Display the line number of the keyword November-december
# sed-n-E '/november-december/= ' gongdatext.txt
9

(6) Add three * * * to the line after Fred begins
# sed-n-E '/^fred/p '-e '/^fred/a\*** ' gongdatext.txt

(7) Filter the line containing Jose and replace Jose with he Xi Gong ' da '.
# sed-n "s/jose/he Xi gong ' da '/P" gongdatext.txt
Jon ' s deloach:408-253-3122:123 Park St., San He Xi gong ' da ', CA 04086:7/25/53:85100
He Xi gong ' da ' santiago:385-898-8357:38 Fife, Abilene, TX 39673:1/5/58:95600


(8) Change Popeye's birthday to 11/14/46
# sed ' 23s/3\/19\/35/11\/14\/46/g ' gongdatext.txt

(9) Delete all blank lines
# sed '/^$/d ' gongdatext.txt

(10) Replace all of the keywords contained inside
# sed ' s/\//-/g ' gongdatext.txt

(11) Please replace the phone number such as: 238-923-8366 in the empty
# sed ' s/-//g ' gongdatext.txt

[Shell Foundation]--sed Command

Related Article

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.