shell_sed statement

sed command
Linux text processing Three Musketeers sed
sed Stream EDite
as a line editor, text editing (editing in units)
Note: sed to edit the file, but do not change the original file;

works of sed:
Specify a text file, in order to read the contents of a text file each line is read into the pattern space (pATTERN sPACE), text matching (regular expression) in the pattern space, the contents of the match modify (replace, delete, print, modify, , save, etc.)

Usage:
sed [OPTION] ... {Script-only-IF-NO-OTHER-Script} [the INPUT-File] ...
sed [parameters] "sed own independent use format statement" [Text File]

Common Options
-n reject the default display
-r specify extended regular expression
matching IP
1, all \ are not 2, | or
-e Script for the while IF
-f specify the script file
-l length specified text wrap?
-i edit files (do not use)

the address delimited embodiment
1, the direct matching value
sed -n '5p' file
2, d ~ step
sed -n '1 ~ 3p' file
3, the end of the specified line $
sed -n '10, $ p 'document
4, / regular /
/ regular 1 /, / n is 2 /
Sed -n' / K \ {. 5, \} /, $ P 'ABC
Sed -n' / \ (FD \) \ + /, + 2p 'abc? regular EDITORIAL no problem, there is a problem written on the back
sed -n'
5,0, addr
addr can be a digital / $ / regular expression
sed -n '0, / k \ . 5 {, \} / P 'ABC
. 6, addr, N +
arranged to addr line after line N addr
sed -n' / fd /, + 5p 'abc

** common commands
p print print (the default is displayed on the screen) used with the advice and -n;
replace c
sed "/ aaa / c \ A" A
d delete
sed "/ aaa / d" A
sed "1, 3D" a
n-N read / pattern matching is added to the contents of the next row line, and then manipulate
w save the contents of the file to a new file
sed "/ patthen / w new file name" name of the original file
sed "/ k / W / tmp / aabbcc "ABC
* S * text Alternatively, the default mode is the first alternative to match space;
syntax: s / pattern / string /
middle / can be replaced by a special character, for example, any of: s # %%%% @@@ S S ##
G all
\ 1 \ 2 and the same packets of regular expressions, implement the reverse application;
designate a replacement to modify a match
& preceding call (pattern) matching content
sed "s /r..t/&er/g "file
w
the p-

advanced application of sed
pattern space - hold space" occupied space "
take their future learning

Exercise:
Delete whitespace characters /boot/grub/grub.conf file trekking first of all leading white space;
sed 'S / ^ [[: Space:]] \ + // G' /boot/grub/grub.conf
delete the / etc / fstab file in the first line of all begin with #, followed by at least one blank character line # and whitespace characters;
sed 'S / ^ # [[: Space:]] \ + // G' / etc / fstab
given a directory, remove its directory name
echo "/ etc / passwd" | sed 's / [^ /] \ + $ //'
given a directory, remove the file name
echo "/ etc / passwd" | sed 's # ^ /. * / \ (. * \) ##

/ var / log / Message
Sed '/ [^ /] \ + /'

dirname - path taken part file path
basename - the file name of the file path portion removed

 

Guess you like

Origin www.cnblogs.com/HZDHH/p/11388387.html