sed yl operation

sed –n ‘p’ /etc/passwd

Performing a plurality of commands:
Sed [Options] {-f-in Commands Sed-File-A-INPUT-File} {}
$ test_script.sed CAT
/ ^ the root / P
/ ^ the nobody / P
Sed -n -f test_script.sed / etc / passwd

sed –e ‘command1’ –e ‘command2’ –e ‘command3’
sed –n -e /^root/ p -e /^nobody/ p /etc/passwd

打印:
sed -n ‘2 p’ employee.txt
sed -n ‘1,4 p’ employee.txt
sed -n ‘2,$ p’ employee.txt
sed -n ‘1~2 p’ employee.txt 1~2 匹配 1,3,5,7,…… 2~2 匹配 2,4,6,8,……
sed -n ‘/Jane/ p’ employee.txt
sed -n ‘/Raj/,$ p’ employee.txt
sed -n ‘/Raj/,/Jane/ p’ employee.txt
sed -n ‘/Jane/,+2 p’ employee.txt

删除:
sed ‘2 d’ employee.txt
sed ‘/^$/ d’ employee.txt
sed ‘/^#/ ‘ employee.txt

写入:
sed ‘w output.txt’ employee.txt
sed -n ‘w output.txt’ employee.txt
sed -n ‘2 w output.txt’ employee.txt

替换:
sed ‘s/Manager/Director/’ employee.txt
sed ‘/Sales/s/Manager/Director/’ employee.txt
sed ‘s/a/A/’ employee.txt
sed ‘s/a/A/g’ employee.txt
sed ‘s/a/A/2’ employee.txt
sed -n ‘s/John/Johnny/p’ employee.txt
sed -n ‘s/John/Johnny/w output.txt’ employee.txt
sed ‘s/john/Johnny/i’ employee.txt

cat files.txt
/etc/passwd
/etc/group

sed ‘s/^/ls -l/’ files.txt
ls -l/etc/passwd
ls -l/etc/group

sed ‘s/^/ls -l /e’ files.txt
-rw-r–r-- 1 root root 1533 Dec 13 20:21 /etc/passwd
-rw-r–r-- 1 root root 682 Dec 13 20:21 /etc/group

sed -n ‘s/manager/Director/igpw output.txt’ employee.txt

定界符:
sed ‘s|/usr/local/bin|/usr/bin|’ path.txt
sed ‘s^ /usr/local/bin^ /usr/bin^’ path.txt
sed ‘s@/usr/local/bin@/usr/bin@’ path.txt
sed ‘s!/usr/local/bin!/usr/bin!’ path.txt

And a reference packet
Sed 'S / [0-9] [0-9] / [&] /' employee.txt
Sed 'S /^.*/<&>/' employee.txt
Sed 'S / \ (. * \). * / \ 1 / 'employee.txt

echo "The Geek Stuff"|sed -n 's/\(\b[A-Z]\)/(\1)/pg' 
(T)he (G)eek (S)tuff 

sed can handle up to nine packets, respectively \ 1 \ 9 represents

GNU Sed proprietary replacement flag
\ l flag
Sed -n 'S / John / JO \ lHNNY / P' employee.txt
101, JOHNNY Doe, the CEO
\ L flag
sed -n 's / John / JO \ LHNNY / p' employee.txt
101, JOHNNY Doe, the CEO
\ U flag
\ the U-flag
\ E flag
Sed -n 'S / John / \ UJohnny \ Boy E / P' employee.txt
101, JOHNNY Boy Doe, the CEO

sed regular expression
^ matches the beginning of each line
end of the line matching $
metacharacter point match any single character except a newline
asterisk matches zero or more of its preceding character
\ + matches one or more times before it characters
? \ match zero or one character in front of it
if you want to search for special characters in regular expressions (eg: positive
.,), it is necessary to use \ to escape their
character set [0-9]
pipe symbol | used to match on both sides of a subexpression any
regular expression to keep behind {m} indicate an exact match of the m-th regular
sed -n '/ [0-9] \ {5 \} / p' numbers.txt
matched to m n times ({ m, n}): regular expressions keep behind {m, n} indicates that an exact match at least the regular m, up to n times. m and n can not be negative, and less than 255. The
regular expression to keep behind {m,} indicates that an exact match at least the regular m
Sed -n '/ [0-9] \ {3,5 \} / P' Numbers .txt
character boundary \ B
Sed -n '/ \ ATHE \ B / P' the words.txt
trackback \ n-
Sed -n '/ (The) \. 1 / P' the words.txt

sed comments begin with #

Review:
Sed -i 'S / John / Johnny /' employee.txt
changes and backup:
Sed -ibk 'S / John / Johnny /' employee.txt

Insert:
command a new row may be inserted after the specified position
sed '2 a 203, Jack Johnson , Engineer' employee.txt
added with \ n to wrap between the multiple rows
sed '/ Jason / a 203, Jack Johnson, Engineer \ n204, Mark Smith, Sales Engineer ' employee.txt
command i inserted before the position of the specified row
sed' 2 i 203, Jack Johnson , Engineer 'employee.txt
command line c may replace the old with the new row
sed' 2 c 202, Jack, Johnson, Engineer 'employee.txt
equivalent to sed' 2s /.*/ 202, Jack, Johnson, Engineer / 'employee.txt

Print invisible characters
CAT tabfile.txt
fname First the Name
lname the Name Last
MNAME the Name Middle

sed -n ‘l’ tabfile.txt
fname\tFirst Name$
lname\tLast Name$
mname\tMiddle Name$

Print line number (commands =)
Sed '=' employee.txt
. 1
101, Johnny Doe, the CEO
2
102, Jason Smith, the IT Manager
. 3
103, Raj Reddy, the Sysadmin

Sed '/ Raj / =' employee.txt
101, Johnny Doe, the CEO
102, Jason Smith, the IT Manager
. 3
103, Raj Reddy, the Sysadmin
multiple files
sed -n '/ root / p' / etc / a / etc / b

Space print mode (Command n-)
Sed n-employee.txt

Published 43 original articles · won praise 0 · Views 3044

Guess you like

Origin blog.csdn.net/oTobias/article/details/103525933
sed