Two. sed
Format: sed ' content ' p-n file print a line of content for English//Surround
Sed ' content ' d file prints other lines except content
sed do not add-I to view add-i to the source file
-R does not need to be de-defined ? + ( ) | You need to
Print the specified line sed ' p-n 1.txt;
does not add-n will appear 2 times per row, printing line 10th
sed ' 1,4 ' p-n 1.txt;
1 to 4 rows
sed ' 5,$ ' p-n 1.txt
5 to the last line
p for printing, D for delete
-n silent mode, used with P. (only lines that match the criteria are printed)
Sed-n '/^\//p '/etc/fstab
A \ String: Appends a new line after the specified line
\ nthe line can be wrapped
i \ String: Ghanio in front of the specified line
R path The specified file is added to the found row out
Sed ' 1,3r/etc/passwd '/etc/fstab
For file merging
W Path writes the find content to the specified file path
Sed ' 1,5w/etc/123.txt '/etc/fstab
s/What to find/replace/default replace is the first one per line
G: Global Substitution
I: Ignore case
s///s### [Email protected]@@
Change
Live-->liver
Love-->lover
s/l. E/&r/g is found to support the regular, but the replacement content is not supported, can only use & instead of the previously found content, followed by a R
S#\ (L.. e\) #\1r#g Latter reference
Live-->live
Love-->love
S#l\ (.. e\) #L \1#g can only be referenced using latter
Sed-f/xssh/1.sh/etc/fstab
Executing files using shell files
Sed-r
Using the extended regular
History |sed ' s#^[[:space:]]# #g ' | Cut-d '-f1
Cut out the beginning of the line
/Internal If there is a conflict between the characters and formatting, "\"/
Print a line containing a string sed-n '/root/' P 1.txt
Rows that contain root
You can use ^. * $ and other special symbols-E can achieve multiple tasks at the same time
Sed-e '/root/p '-e '/body/p '-n 1.txt can also be used;
Sed '/root/p; /body/p '-N 1.txt
Delete Row sed '/root/d ' 1.txt;
Delete the row with root, no need to take the-n
Sed ' 1d ' 1.txt;
Delete the first line
Sed ' 1,10d ' 1.txt
Delete lines 1 to 10
Replace sed ' 1,2s/ot/to/g ' 1.txt,
1, 2 rows ot changed to
where S is the meaning of the substitution, G is the global substitution, otherwise only the first one is replaced,
/can also be #, @ and so on
Sed ' s/bash/123/gi ' 1.log
Replace time plus I is case insensitive
Remove all digital sed ' s/[0-9]//g ' 1.txt
Remove all non-digital sed ' s/[^0-9]//g ' 1.txt
Head-n2 1.txt |sed ' s/\ (root\) \ (. *\) \ (bash\)/\3\2\1/'
Swap two string positions
Sed-r ' s/(^.*) (:. *:) (\/.*$)/\3\2\1/g '
Sed-r ' s# (^.#) (:. *:) (/.*$) #\3\2\1# '
Modify file contents directly sed-i ' s/ot/to/g ' 1.txt
Sed Practice Questions:
Copy/etc/passwd to/root/test.txt, print all lines with SED
Sed-n ' 1, $p ' test.txt
Print 3 to 10 lines of test.txt
Sed-n ' 3,10p ' test.txt
Print the line containing ' root ' in test.txt
Sed '/root/p '-n test.txt
Delete Test.txt 15 rows and all subsequent rows
Sed ' test.txt, $d '
Delete rows containing ' bash ' in Test.txt
Sed '/bash/d ' test.txt
Replace ' root ' as ' Toor ' in Test.txt
Sed ' s#root#toor#g ' test.txt
Replace Test.txt '/sbin/nologin ' as '/bin/login '
Sed ' s#/sbin/nologin#/bin/login#g ' test.txt
Delete all the numbers in rows 5 through 10 in Test.txt
Sed ' 5,10s#[0-9]# #g ' test.txt
Delete all special characters in test.txt (except for numbers and uppercase and lowercase letters)
Sed ' s#[^0-9a-z]# #g ' 123
Swap the first and last words in the test.txt position
Sed-r ' s# (^.*) (:. *:.*:.*:.*:.*:.*/.*/) (. *$) #\3\2\1#g ' Test.txt
Bash:x:0:0:root:/root:/bin/root (last word with first swap)
Sed-r ' s# (^.*) (:. *:.*:.*:.*:.*:.*) (/.*/.*$) #\3\2\1#g ' Test.txt
/bin/bash:x:0:0:root:/root:root (the last shell and the first word exchange)
Sed ' s#\ (^.*\) \ (: x:.*\) \ (/.*$\) #\3\2\1#g ' test.txt (using de-meaning)-R can omit the caret character
Replace the first number and the last word that appear in the test.txt position
Sed-r ' s# (^.*) ([0-9]) (. */.*/.*/) (. *$) #\1\4\3\2#g ' Test.txt
Sed-r ' s# (^.*) ([0-9]) (. */.*/.*/) (. *$) #\1\4\3\2#g ' Test.txt
Move the first number in the Test.txt to the end of a line
Sed-r ' s# (^.*:) ([0-9]) (. *$) () #\1\4\3\2#g ' 123
Test.txt 20 to the end of the line Plus ' AAA: '
Sed ' 20s/$/aaa/g ' 123
Awk
intercept a section of the document Awk-f ': ' {print $} ' 1.txt
You can also use a custom character to connect each segment Awk-f ': ' {print $ "#" $ "#" $ $ "#" $4} ' 1.txt
match character or string awk '/oo/' 1.txt
match awk-f ': ' ~/oo/' 1.txt for a segment
multiple matches awk-f ': '/root/{print $1,$3}; $ ~/test/; $ ~/20/' 1.txt
conditional operator = =, >,<,!=,>=;<=
awk-f ': ' $3== ' 0 "' 1.txt;
awk-f ': ' $3>= ' 1.txt;
awk-f ': ' $7!= '/sbin/nologin ' 1.txt;
awk-f ': ' $3<$4 ' 1.txt;
awk-f ': ' $3> ' 5 "&& $3<" 7 "' 1.txt
awk-f ': ' $3> ' 5 "| | $7=="/bin/bash "' 1.txt
awk built-in variable NF (number of segments) NR (number of lines)
head-n3 1.txt | awk-f ': ' {print NF} '
head-n3 1.txt | awk-f ': ' {print $NF} '
head-n3 1.txt | awk-f ': ' {print NR} '
print 20 lines after the line awk ' nr>20 ' 1.txt
awk-f ': ' nr>20 && ~/ssh/' 1.txt
change the value of a segment awk-f ': ' $1= ' root ' 1.txt
mathematical calculations, add the third and fourth values and give the seventh paragraph Awk-f ': ' {$7=$3+$4; print $} ' 1.txt
calculates the sum of the third paragraph awk-f ': ' {(tot=tot+$3)}; END {print tot} ' 1.txt
awk can also use if keyword awk-f ': ' {if ($1== "root") print $} ' 1.txt
awk-f ': ' {print '} ' 1.log
by: Split Show first column
Print $
Show All
awk-f ': ' $1~/root/' 1.log
the first column matches the root display all columns
{print NR}
Show individual columns nr to show line numbers
awk-f ': ' $3==0 ' 1.log
The third segment of the match is 0.
>= greater than or equal to
! = does not equal
$3<$4 matches this comparison.
$3==$4 requires 2 equals sign
1 equals is the value of an assignment
$3==$4 && &1==$2 && and ~
awk-f ': ' {print $NF} ' 1.log
at this point NF equals 7
' nr>20 && $NF! = "/sbin/nologin" '
after 20 rows and 7 columns are not equal
awk-f ': ' {(tot=tot+$3)}; End{print tot} ' 1.log
Add the third column consecutively
awk-f ': ' {(if $1== "root") print $} ' 1.log
row of the first column root
awk-f ': ' $7=$3+$4 ' 1.log
7th column equals 3 4 columns added
awk Exercises
print the entire test.txt with awk (the following is done with the awk tool for Test.txt)
Find all rows that contain ' bash '
use ': ' as a delimiter to find a line with a third paragraph equal to 0
Use ': ' As a delimiter to find the first line of ' root ' and replace the ' root ' of that segment with ' Toor ' (which can be used together with SED)
use ': ' As a delimiter to print the last paragraph
print all rows with a number of rows greater than 20
use ': ' As a delimiter to print all third paragraphs less than the fourth paragraph
use ': ' As a delimiter, print the first paragraph and the last paragraph, and the middle with ' @ ' connection (for example, the first line should be the form of ' [email Protected]/bin/bash ')
use ': ' As a delimiter to add the fourth paragraph of the entire document, summing
This article is from the "you and I Walk" blog, please be sure to keep this source http://ondali.blog.51cto.com/6650368/1615965
Text file lookup, regular sed awk