grep, g/re/p , (Global / Regular Expression(RE) search / and Print Out the line) ,全面搜索正则表达式并把行打印出来 ,是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。用于过滤/搜索的特定字符。可使用正则表达式能配合多种命令使用,使用上十分灵活。
grep searches each FILE for PATTERNS. PATTERNS is a string that contains one or more patterns separated by newline characters, and grep prints each line that matches a pattern. When using grep in a shell command, PATTERNS should usually be quoted.
A FILE of “-” denotes standard input. If no FILE is specified, recursive searches examine the working directory, while nonrecursive searches read standard input.
Furthermore, the variant programs egrep, fgrep, and rgrep are equivalent to grep -E, grep -F, and grep -r, respectively. These variants are obsolete, but remain available for backward compatibility.
Grep 用法
grep [OPTION…] PATTERNS [FILE…]
grep [OPTION…] -e PATTERNS … [FILE…]
grep [OPTION…] -f PATTERN_FILE … [FILE…]
WILDCARDS
.
Match any Character Except New Line
?
Match 0 or One characters
*
Match 0 or More characters
+
Match 1 or More characters
QUANTIFIERS
{n}
Matches exactly n times
{n,}
Matches n or More
{,m}
Matches up to m (maximum)
{n,m}
Matches between n and m (Minimum, Maximum)
CHARACTER CLASSES
[A-Za-z] Any lower and upper case letters [0-9] Any Number [0-9A-Za-z] Any lower and upper case letter or digits
Sed Commands Options Description Exit sed without processing any more commands or input. Delete the pattern space; immediately start next cycle. Print out the pattern space (to the standard output). Appending text after a line. insert text before a line. = Print out the current input line number (with a trailing newline). Replaces the line(s) with text. w Write the pattern space to filename. Empty the content of the pattern space. Delete text in the pattern space up to the first newline , restart cycle if pattern space contains newlines. Execute the command that is found in the pattern space. F Print the file name of the current input file. Reads file filename. n N Read/append the next line of input into the pattern space. W Write the first line of the current pattern space to filename Exchange the contents of the hold and pattern spaces. Transliterate any characters in the pattern space.
Command Options Options Description -i[SUFFIX] Edit files in place (make backup if SUFFIX supplied). -n Suppress automatic printing of pattern space. -f add the contents of script-file to the commands to be executed -e Execute multiple sed commands -r or -E Use extended regular expressions (supports ‘+’, ‘(’, ‘)’)■ -s Consider files as separate rather than as a single, continuous stream. -c Copy the input to the standard output. -I pecify the desired line-wrap length for the I command. Command Flags Flags Description g Global substitution - Replace all occurrences in each line. Execute shell command after substitution. w Save only the substituted lines to a specified file. Perform case-insensitive pattern matching. p Print only the substituted lines. 1,2,4,… Substitute the nth occurrence- Replace specific occurrence of a pattern.
BASIC EXAMPLES
$ seq |sed $ seq | sed -n $ sed ‘/warning/i # This is a warning’ logfile $ cod -o ‘<c/corvor/now-corvor/;c/port/8080/;}’ config.-conf
REPLACING TEXT $ sed s/old/new/g file.txt $ sed ‘4 s/old/new/’ file.txt $ sed s/old/new/3 file.txt $ sed ‘s/A//’ file.txt $ sed -n 7important/w important_lines.txt’ logfile $ sed 7hello/s/world/universe/’ file.txt
APPEND AND PREPEND TEXT $ sed -n 7hello/p’ file.txt $ sed -n 7hello/lp’ file.txt $ sed -n 7hellQ/lp’ file,txt $ sed ‘2a Text after line 2’ file.txt $ sed ‘\(a THE END!' file.txt\) sed ’s/old/new/’ config.conf
• • • DELETING LINES $ sed 1,4d’ file.txt $ sed ‘6~4d’ file.txt $ sed ‘\(d' file.txt\) sed 7 AErrors/d’ file.txt $ sed 7 A\(/d' file.txt\) sed 7 A#/d’ file.txt $ sed ’3d’file t¥t
• • • OPTIONS EXAMPLES $ sed -n 7error/p’ syslog.log $ sed -e ‘s/server-01/server-02/’ -e ‘s/port=8080/port=9090/’ app.conf $ sed -i.bak ‘s/v3.3.2/3.4.1/’ app.conf $ sed .r ‘s/(error|warning)/info/’ syslog Ing $ bed -b ‘b/liubuidiiie/bei vei/1 /eiu/Luunf $ sed -n -e ’1,5C' -e’ I his is a new line.’ source.txt destination.txt $ seo -T repiace_pauerns.sea conng.ixi sysxplore.com $ sed -I long_text.txt