SHELL usage seven (Sed statement)

1, SHELL statement programming Sed real case

1) SHELL programming four swordsman bis Sed tools, Linux system is mainly used to edit text files,
Syntax | (vim interactive mode editor vi), Sed tools, printing, also known as non-interactive mode editor
format:
sed -option (parameters) 'Command' (command) File (file object)
 Option parameters: -i (modified), - n (silent mode), - e (extended), - l, -b, -c;
 Command Command: p (print), d (delete), N (multi-line), G, D;
2) SHELL programming four swordsman Sed operating a tool case, tool-based user Sed list.txt file
The name was changed to root jfedu, the command is as follows:
sed s/jfedu/root/g list.txt
sed 's#jfedu#root#g' list.txt
sed -i 's#jfedu#root#g' list.txt
sed -e '3s/2/100/g' -e '4s/3/100/g' list.txt
sed '3s/2/100/g;4s/3/100/g' list.txt
sed '3,$s/11/100/g' list.txt3)替换jfedu.txt文本中old为new:
sed
's/old/new/g'
jfedu.txt

 

4) Printing jfedu.txt the first to third rows of text:
sed    -n '1,3p'      jfedu.txt

 

5) Print jfedu.txt text in the first line and the last line:
sed   -n '1p;$p'     jfedu.txt

 

6) Delete jfedu.txt first to third rows, delete rows to match the last line:
sed       '1,3d'     jfedu.txt
sed     '/jfedu/,$d'   jfedu.txt

 

7) Delete jfedu.txt last six rows and delete the last line:
for
i in `seq 1 6`;do sed -i
'$d' jfedu.txt ;done
sed    '$d'  jfedu.txt

 

8) delete the last line jfedu.txt:
sed   '$d'   jfedu.txt

 

9) Find jfedu in jfedu.txt row and add the word character in the next line, a representation in its next
Line add the string:
sed    ' / jfedu / aword '     jfedutxt

 

10) Find jfedu row in jfedu.txt, and add the word characters on a line, i said in its on
Add a line string:
sed      ' / jfedu / Nord '     jfedu.txt

 

11) Find jfedu.txt end of the line to the end of the test string is added Word, $ indicates the end identification, in &
Sed said adding:
sed    's/test$/&word/g'    jfedu.txt

 

12) Find www in jfedu.txt line, the line in its first add a string word, ^ represents the starting identity,
& In Sed said adding:
sed     '/www/s/^/&word/'     jfedu.txt

 

13) a combination of a plurality of command sed using -e parameters:
sed -e '/www.jd.com/s/^/&1./' -e 's/www.jd.com$/&./g'   jfedu.txt
14) a combination of a plurality of command sed, semicolon ";" Segmentation:
sed -e '/www.jd.com/s/^/&1./;s/www.jd.com$/&./g' jfedu.txt

 

15) Sed reading system variables, variable substitution:
WEBSITE=WWW.JFEDU.NET
Sed “s/www.jd.com/$WEBSITE/g” jfedu.txt

 

16) Modify Selinux enforcing policies for the disabled, Find / SELINUX / line, then the line
enforcing value change disabled, s represents the line does not include SELINUX!:
sed -i     '/SELINUX/s/enforcing/disabled/g' /etc/selinux/config
sed -i    '/SELINUX/!s/enforcing/disabled/g' /etc/selinux/config

 

17) The combined upper and lower rows, i.e. two lines were combined:
sed     'N;s/\n/ /'     jfedu.txt
18) the line number of the print document list.txt command follows:
for i in `seq 1 $(cat list.txt |wc -l)`;do sed -n "${i}s/^/&$i) /p" list.txt ;done
sed = list.txt |sed 'N;s/\n/) /g'
2, SHELL statement programming Grep real case
1) Find the path Linux operating system file and directory search, query tool, and Grep is Linux
Under the text file content retrieval system or matching tool, Grep called global regular expression search tools,
It has been widely adopted in the enterprise.
2) Grep tool syntax: grep -option (parameters) 'word' (Image) File
(Text file);
 Option parameters: -a (text), - c (statistics), - i (ignore case), - n (line number),
-v (unselected), - o (match only), - w (phrase), - A (after the match), - B (matching of
Before), - C (before and after the match); 3) Grep real case of a company, Find / www user information whether there etc / passwd file,
Command as follows:
grep root /etc/passwd
grep "root" /etc/passwd
grep -w "^root" /etc/passwd
grep -i "root" /etc/passwd

 

4) Grep real case of two companies, ifconfig see card information, only to see the IP address of the row information,
Command as follows:
ifconfig|grep netmask
ifconfig|grep -w inet
ifconfig|grep -w 255
ifconfig|grep -E "172|127"
ifconfig|grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}"

 

# Create three files, and the file is written to a different code, respectively;
for i in `seq 1 3`;do touch jf$i.txt;echo jfedu jf$i >jf$i.txt;echo cat
jf$i.txt;cat jf$i.txt;echo ----------;done

 

5) Detailed wildcard type: *
Zero or more characters, numerals;
 ? 
Match any one character; 
# 
represents a comment;
 | 
pipe symbol; 
; 
a plurality of commands performed continuously;
 & 
background operation command;
 ! 
Logic operation non; 
[] 
content range, the content matching brackets; 
{} 
command block, a plurality of matching commands.

 

6) Detailed regular expressions:
* 
Previous character matches zero or more times; 
. 
Match any character except newline; 
. * 
Represents any character;
 ^ 
matches the beginning of the line that begins with a character; 
$ 
matches the end of a line that ends with a character ; 
\ (.. \) 
tag-matching characters; 
[] 
matches any given in parentheses characters, but only one character match; 
[ ^ ] 
matches any other than a character in brackets; 
\ 
escape, cancel the special meaning; 
\ < 
start anchor word; 
\ > 
end anchor word; 
{n} 
matches the character appears n times; {n,} 
matches the character appears greater than or equal to n times; 
{n, m} 
matches the character appears at least n times, most appear m times; 
\ w 
matching text and numeric characters, symbols do not match; 
\ W is 
\ w inverted form of non-word matches one or more characters matching 
symbols; 
\ B 
word lock operator; 
\ S 
matches any whitespace character;
\ d 
matches a numeric character, equivalent to [ 0 - . 9 ].

 

7) Common GREP tool business case exercise columns:
grep -c " test " 
jfedu.txt statistical test of rows of characters; 
grep -i " the TEST " 
jfedu.txt case-insensitive search for all line the TEST; 
grep -n " test " 
jfedu.txt print line and the line number of test ; 
grep -v " test " 
jfedu.txt not print test line; 
grep  " test [53 is] " 
jfedu.txt test starts with the character, by row 3 or 5; 
grep  " ^ [^ test] " 
jfedu.txt Show output test line is not the first line; 
grep  " [of Mm] AY " 
jfedu.txt 
match line m or the beginning of m; 
grep  "K…D"
jfedu.txt 
matching K, any three characters, line immediately D; 
grep  " [AZ] [. 9] D " 
jfedu.txt 
matching uppercase character line immediately. 9D; 
grep  " T \ {2, \} " 
jfedu.txt printing characters T character twice or more consecutive rows; 
grep  " T \ {. 4, 6 \} " 
jfedu.txt character printing characters T 4 and 6 times of consecutive rows; 
grep -n " ^ $ " 
jfedu.txt 
print the line number where the blank lines; 
grep -vE " # | ^ $ " 
jfedu.txt 
does not match the file # and blank lines; grep 
--color -ra - E
 " db | config | SQL " * match or comprising db or config 
files to sql; 
grep
-E --color " \ <([0-9] {l, 3} \.). 3} {([0-9] {l, 3}) \> " 
jfedu.txt matching 
IPV4 address.

 

Guess you like

Origin www.cnblogs.com/nsh123/p/12157674.html