Usage of sed in linux (addition and deletion of row data), linuxsed

Source: Internet
Author: User

Usage of sed in linux (addition and deletion of row data), linuxsed

Sed syntax
[Root @ fwq test] # sed -- help
Usage: sed [Option]... {script (if no other script exists)} [input file]...

-N, -- quiet, -- silent cancel Automatic print mode space
-E script, -- expression = Add "script" to the program running list
-F script file, -- file = script file: Add "script file" to the program running list
-- Follow-symlinks follow symlinks when processing in place; hard links will still be broken.
-I [SUFFIX], -- in-place [= SUFFIX]
Edit files in place (makes backup if extension supplied ).
The default operation mode is to break symbolic and hard links.
This can be changed with -- follow-symlinks and -- copy.
-C, -- copy
Use copy instead of rename when shuffling files in-I mode.
While this will avoid breaking links (symbolic or hard),
Resulting editing operation is not atomic. This is rarely
The desired mode; -- follow-symlinks is usually enough, and
It is both faster and more secure.
-L N, -- line-length = N specifies the expected length of line feed for the "l" command.
-- Posix: Disable all GNU extensions
-R, -- regexp-extended use an extended regular expression in the script
-S, -- separate treats the input file as an independent file rather than a long continuous input
-U, -- unbuffered reads the least data from the input file and refreshes the output more frequently.
-- Help print help and exit
-- Version: Output version information and exit

 

 

Print the travel number and delete 2-5 rows

[Root @ fwq test] # nl/etc/passwd | sed '2, 5d '| more
 
1 root: x: 0: 0: root, 704,03738888766, 03738888766:/root:/bin/bash
6 sync: x: 5: 0: sync:/sbin:/bin/sync
7 shutdown: x: 6: 0: shutdown:/sbin/shutdown
8 halt: x: 7: 0: halt:/sbin/halt
9 mail: x: 8: 12: mail:/var/spool/mail:/sbin/nologin
10 uucp: x: 10: 14: uucp:/var/spool/uucp:/sbin/nologin

Print the travel number and delete only 2-5 rows

[Root @ fwq test] # nl/etc/passwd | sed '2d '| more
1 root: x: 0: 0: root, 704,03738888766, 03738888766:/root:/bin/bash
3 daemon: x: 2: 2: daemon:/sbin/nologin
4 adm: x: 3: 4: adm:/var/adm:/sbin/nologin
5 lp: x: 4: 7: lp:/var/spool/lpd:/sbin/nologin

Print the travel number and delete all content after line 1

[Root @ fwq test] # nl/etc/passwd | sed '2, $ d'
1 root: x: 0: 0: root, 704,03738888766, 03738888766:/root:/bin/bash
[Root @ fwq test] #

Print the travel number and add "drink tea" to the end of row 2nd"

[Root @ fwq test] # nl/etc/passwd | sed '2a drink tea '| more
1 root: x: 0: 0: root, 704,03738888766, 03738888766:/root:/bin/bash
2 bin: x: 1: 1: bin:/sbin/nologin
Drink tea
3 daemon: x: 2: 2: daemon:/sbin/nologin
4 adm: x: 3: 4: adm:/var/adm:/sbin/nologin

Print the travel number and add two lines of "drink tea or drink beer"

[Root @ fwq test] # nl/etc/passwd | sed '2a drink tea or ...\
Drink beer? '| More
1 root: x: 0: 0: root, 704,03738888766, 03738888766:/root:/bin/bash
2 bin: x: 1: 1: bin:/sbin/nologin
Drink tea or...
Drink beer?
3 daemon: x: 2: 2: daemon:/sbin/nologin
4 adm: x: 3: 4: adm:/var/adm:/sbin/nologin

Print the travel number and replace the content in line 2-5 with "No 2-5"

[Root @ fwq test] # nl/etc/passwd | sed '2, 5c No 2-5 number' | more
1 root: x: 0: 0: root, 704,03738888766, 03738888766:/root:/bin/bash
No 2-5 number
6 sync: x: 5: 0: sync:/sbin:/bin/sync
7 shutdown: x: 6: 0: shutdown:/sbin/shutdown
8 halt: x: 7: 0: halt:/sbin/halt

Print the travel number and only list the content in line 5-7

[Root @ fwq test] # nl/etc/passwd | sed-n'5, 7p'
5 lp: x: 4: 7: lp:/var/spool/lpd:/sbin/nologin
6 sync: x: 5: 0: sync:/sbin:/bin/sync
7 shutdown: x: 6: 0: shutdown:/sbin/shutdown

 


(Urgent) in LINUX, use sed to delete a specified row. The starting position of the specified row is obtained through parameters. How can I delete the end position of the file?

Sed $ I ', $ d' input

I is a shell variable. It must be obtained outside the sed scope (that is, outside single quotes) to be correctly parsed. Or, in a simpler way, change single quotes to double quotation marks.
In addition, if you want to directly modify the file, you need to use the sed-I option, that is, sed-I $ I ', $ d' input

For the linux sed command, delete multiple lines. One line contains two keywords and the other contains two keywords.

I can't figure out how to write a sentence with sed. You have written a script to implement the functions you want.
If you are sure that <package 10014 to/package> is 5 rows.

#! /Bin/bash
Filename = file
LINES = 'grep-n' sharedUserId = "10014" '$ filename | wc-l'
While [[$ LINES-ne 0]
Do
STARTnum = ('grep-n' sharedUserId = "10014" '$ filename | awk-F ":"' {print $1 }'')
For I in $ {STARTnum [0]}
Do
J = 'expr $ I + 4'
Sed-I ''$ I ',' $ jd' file
Done
(LINES --))
Done
#######################
Try sed-I '/^ <package. * 10014/,/^ <\/package/d'

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.