sed & awk 101 hacks study notes -sed

sed basic syntax:
sed Options 'Commands' file
sed file is first read from the first row, then all of Commands executed;
and then read a second row, performs all sed-commands;
This process is repeated until the end of the input-file.
(Options are optional, command must be added, if the direct sed file will be reported missing out commands, commands of '' can not be added)

commands may be one or more of

p: print command

A is the simplest form as:
Sed -NP file: print content to the standard output file of all
the plurality of slightly more complex, as an example to two, and may be in the form command1 & commmand2 also may command1 | command or form, such as:
sed -n '/ hi / p' file: file matching the printing of hi-line
sed -n -e '/ hi / p ' -e '/ hello / p': Print file to match hello or hi-line
while many commands, {} may be used to wrap all the commands, a command line:
Sed -n '{
/ Hi / P
/ Hello / P
/ World / P
}'
when comands many, and sometimes need to modify the post above more practical approach is to write all the commands in the file file1, call the file with the -f:
At this point sed syntax becomes:
sed -f file1 file Options

internal process execution sed to see another blog post:
https://blog.csdn.net/oTobias/article/details/99591929

(/ Hi / is a row of matching hi, / hi / is not a command, execute sed -n '/ hi /' file will report missing commands)

A row address range:
between two lines separated by a comma
sed -n '1,4p' file: Print all rows 1-4 between rows
sed -n -e '1p' -e ' 4p' file: print only 1 row 4 and row
sed -n '/ hi /, / hello /' file to print to all the lines between the matching and hi Hello
Sed -e -n '/ hi / P' -e '/ Hello / P' File: only hi and hello to print matching lines

n ~ m address range: Start from n rows, each row plus m

sed -n 1 ~ 2p file print 13579 ... OK

$: Represents the last line
using $ row represents the last note in single quotation marks '' or else sed performs variable substitution

sed -n 1, $ p file: p variable will automatically find, replace the value of p, p If no value is defined, and will be reported p: undefined variable, if previously defined the value of p, as set p = 2, will become sed -n 1,2 file, which is wrong, because there is no command, reported missing commands.
So we divided into two cases:

Last line: sed -n '1, $ p' file: print to the last line of content 1

Variable substitution: sed -n "1, $ p" p file: Printing 1-2 line content

(Summary: replace the invalid $ variable within single quotes, double quotes and can enter into force)
character address range:
character address range used in the // indicates the address range
^: Starts
$: end character

sed -n '/ ^ $ / p' file: printing a blank line (this also requires single quotes outside, particularly unclear reasons, special character table in short range according to the situation when the commands applied in single or double quotation marks)

d: Delete command

(Note that it only removes the contents of the pattern space, sed and other commands, command d does not modify the original contents of the file.)
Sed 2d File: model space of the second row is deleted, the other line printing pattern space
if you do not provide the address range, sed default match all lines
sed d file mode space all the rows are deleted, printed as empty

wnewfile:写command

sed wnewfile file: the file copy is newfile (the default screen print file content)
sed -n wnewfile file: the file copy is newfile (not to print file contents on the screen)

s: Replace command

g: global flag

1,2,3 ... digital flag (flag indicating occurrence of 1,2,3)

i: ignore case flag

e: Run flag (the flag may be anything as shell space mode command execution)

//: default delimiters
|| @@: optional delimiter

&: Matches reference in replacement
sed 's /^.*/<&>/' file: Add the contents of each line outside <>
(): reference packet (a plurality of packets respectively references, \ 1 refers to the first packet )

Regular Expressions

^: The first line
$: end of the line
: any single character
...: any two characters
: match 0 or more times
+: Match 1 or more times
\? : Match 0 or 1
[]: brackets character set, matching brackets any character
{m, n}: Match m to n times
\ B: character boundary (\ BXX XX \ B \ BXX \ B)
\ n : matching reference
sed -n '/ (the) \ 1 / p' file \ 1 references the, so the two match The
Sed -n 'S / (.
,) (. ,) (. / \ 2, \. 1) , \ 3 / p 'file: exchange column 1 and column 2 content

The sed command as interpreter uses:
To implement this function, you need sed script initially join "# / bin / sed -f! "

VI myscript.sed $
#! / Bin / Sed -f
# exchanging a first column and a second column
Sed -n 'S / (. ), (. ), (. ) / \ 2, \. 1, \. 3 /' File P
# whole line into the <> in the
S / ^.
/ <&> /

! # / bin / sed -nf default output can be masked
initial file as follows:
Here Insert Picture Description
Sed script follows
Here Insert Picture Description
Ends execute
Here Insert Picture Description
-i: option inserted
directly modify the contents of the input file (may also be used to redirect)

a: The next line Insert Command
Sed '2 A hello' File: hello inserted after the second row
i: Inserts on line Command
C: content
sed '2 c hello' file into the second row of content hello
Y: Character conversion
sed 'y / a / b / ' file will be replaced by a B
Sed 'Y / abcde / ABCDE /' File into all corresponding abcde ABCDE, when a plurality of character conversion, the length must be consistent

Published 43 original articles · won praise 0 · Views 3046

Guess you like

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