Common commands and syntax for Grep,awk and SED

Source: Internet
Author: User

Common command syntax for grep

1. Double quote quotes and single quote references
when you enter a string parameter in the G r e P command, it is best to enclose it in double quotation marks. For example: "M y s t r i n g". There are two reasons for this, one is to avoid being misunderstood as the S H e l command, and the second is to find a string consisting of multiple words, for example: "Jet Plane", if it is not enclosed in double quotation marks, then the word p l a n e will be mistaken for a file, the query result will return "file does not exist" error 。 You should also use double quotes when calling variables, such as: G R E P "$ M Y VA r" file name, if not, no result will be returned. Single quotes should be used when calling pattern matching. [[email protected]]# echo ' grep 123 111.txt ' (#注意是反单引号)

2. The commonly used grep options are:
-C outputs only the count of matching rows.
- I is case-insensitive (only for single-character).
-H does not display a file name when querying multiple files.
-L Only output file names that contain matching characters when querying multiple files.
-n Displays matching lines and line numbers.
-S does not display error messages that do not exist or have no matching text.
-V Displays all lines that do not contain matching text.

3. Special--Query in multiple files
$ grep "Sort" *.doc (Find string "s O r T" in #在当前目录下所有. D o C file)
$ grep "Sort It" * (#或在所有文件中查询单词 "sort It")
all of the following examples refer to querying in a single file

4. Row matching
$ grep-c "48″DATA.F
$4 (#g r e P Returns the number 4, meaning that there are 4 rows that contain the string "4 8". )
$ grep "48″DATA.F (#显示包含" 4 8 "string of 4 lines of text)

5. Displays the number of rows that satisfy the matching pattern:
[email protected] oid2000]# grep-n 1234 111.txt
1:1234
3:1234ab

6. Exact Match
[[email protected] oid2000]# grep "1234\>" 111.txt
1234

7. Query for empty rows, and query for rows that begin or end with a condition.
use a combination of ^ and $ to query for blank lines. Use the-n parameter to display the actual number of rows
[[email protected] oid2000]# grep-n "^$" 111.txt (return result 2: #说明第二行是空行)
[email protected] oid2000]# grep-n "^ABC" 111.txt (#查询以abc开头的行)
[email protected] oid2000]# grep-n "abc$" 111.txt (#查询以abc结尾的行)

8. Match Special characters and query for characters that have special meanings, such as $. ' "* [] ^ | \ + ? , you must add \ before a specific character.
[[email protected] oid2000]# grep "\." 111.txt (#在111. txt in the query contains "." of all rows)
[[email protected] oid2000]# grep "my\.conf" 111.txt (#查询有文件名my. c o n f lines)

9. Querying the directory
[email protected] oid2000]# ls–l |grep "^d" (#如果要查询目录列表中的目录)
[email protected] oid2000]# ls–l |grep "^d[d]" (#在一个目录中查询不包含目录的所有文件)
[email protected]]# ls–l |grpe "^d.....x. X "(#查询其他用户和用户组成员有可执行权限的目录集合)

10. Exclude Yourself
ps-ef|grep telnet | grep-v grep (Extracts the "telnet" process in the displayed process; and discards the grep process in PS)
Common command syntax for awk
The awk Command specializes in formatting messages or extracting packets from a large text file, which is the basic syntax of the command
awk [-F Filed-separator] "commands" Input-file (s)
[-F-field delimiter] is optional, a W K uses a space as the default domain delimiter, if the file to be processed with a colon as the partition domain (such as the passwd file), then in the processing time to specify the Awk–f:command input-file (s)

1.1 fields and records
when a W K executes, its browse domain is marked as $1,$ 2 ... $ n. This method is called the domain identifier. With $1, $3 refers to the 1th and 3rd fields, and note that the fields are delimited by commas. If you want to print all fields of a record with 5 fields, you do not have to specify $1, $2, $3, $4, $5, and you can use $0, which means all domains.

1.2 Saving awk output
$ Awk ' {print $} ' input-files > Out-files (#重定向保存输出)
$ Awk ' {print $} ' input-files | tee out-files (#使用t e-e command, output to a file while output to the screen)

1.3 Examples of commonly used awk commands
[email protected]/]# awk ' $ ~/user/'/etc/passwd (#如果某域含有user就将该行打印出来)
rpc:x:32:32:portmapper RPC User:/:/sbin/nologin
rpcuser:x:29:29:rpc Service User:/var/lib/nfs:/sbin/nologin
[email protected]/]# awk '/user/'/etc/passwd (#同上)
[email protected]/]# awk-f: ' {if ($/user/) print $} '/etc/passwd (#如第五域有user则输出该行)
rpc:x:32:32:portmapper RPC User:/:/sbin/nologin
[[email protected]/]# ifconfig | awk '/inet/{print "(#从ifconfig的输出中抽取含inet的行并打印第二域)
[[email protected]/]# ifconfig | awk '/inet/{print $} ' | awk-f: ' {print $} ' (#在上面的基础上再抽取, this command allows you to get the IP address of the machine directly)

Common command syntax for sed

SED is a non-interactive text flow editor. It edits the text copy of the exported file or standard input.

1. Matching of rows
[[email protected]/]# sed-n ' 2p '/etc/passwd print out line 2nd
[[email protected]/]# sed-n ' 1,3p '/etc/passwd print out lines 1th to 3rd
[[email protected]/]# sed-n ' $p '/etc/passwd print out the last line
[[email protected]/]# sed-n '/user/' p/etc/passwd print out the line containing the user
rpc:x:32:32:portmapper RPC User:/:/sbin/nologin
rpcuser:x:29:29:rpc Service User:/var/lib/nfs:/sbin/nologin
[[email protected]/]# sed-n '/\$/' p/etc/passwd print out a line containing $ metacharacters, $ meaning the last line

2. Inserting text and additional text (inserting new lines)
[[email protected]/]# sed-n '/ftp/p '/etc/passwd print out the line with FTP
ftp:x:14:50:ftp User:/var/ftp:/sbin/nologin
[[email protected]/]# sed '/ftp/a\ 456′/etc/passwd insert a new row after the line containing the FTP with the contents 456
[[email protected]/]# sed '/ftp/i\ 123′/etc/passwd a new row in front of the line containing the FTP, the content is 123
[[email protected]/]# sed '/ftp/i\ ' 123″ '/etc/passwd insert a new line in front of the line containing the FTP, in the "123″
[[email protected]/]# sed ' 5 a\ 123′/etc/passwd insert a new row after line 5th with the contents 123
[[email protected]/]# sed ' 5 i\ "12345" '/etc/passwd insert a new line before line 5th with the content "12345"

3. Delete text
[[email protected]/]# sed ' 1d '/etc/passwd delete line 1th
[[email protected]/]# sed ' 1,3d '/etc/passwd delete line 1th to 3rd
[[email protected]/]# sed '/user/d '/etc/passwd delete line with user

4. Replace text, replace command replaces the specified pattern with the replacement mode, in the form:
[a D D r e s S [, address]] S/pattern-to-find/replacement-pattern/[g p w N]
[[email protected]/]# sed ' s/user/user/'/etc/passwd replace the 1th user with User,g to indicate a global replacement
[[email protected]/]# sed ' s/user/user/g '/etc/passwd replace all user with user
[[email protected]/]# sed ' s/user/#user/'/etc/passwd replace 1th user with #user, e.g. for shielding effect
[[email protected]/]# sed ' s/user//'/etc/passwd replace the 1th user with an empty
[[email protected]/]# sed ' s/user/&11111111111111/'/etc/passwd if you want to attach or modify a long string, you can use the (&) command,& command to save the discovery mode in order to recall it, and then put it in the replacement string, here is the & put in front
[[email protected]/]# sed ' s/user/11111111111111&/'/etc/passwd here is to put & back

5. Quick Line Command
here are some line command sets. ([] denotes a space, [] denotes t a b key)
' s/\. $//G ' delete ends with a period line
'-e/abcd/d ' delete the row containing a B c D
' s/[] [] [] */[]/G ' Remove one to the sky, with a space instead
' s/^ [] [] *//G ' Delete line beginning space
' s/\. [] [] */[]/g ' Delete period followed by two or more spaces, replaced by a space
'/^ $/d ' Delete empty lines
' s/^.//G ' Delete the first character
' S/col \(...\)//g' Delete the three letters immediately following C O l
' s/^ \///G ' Remove the first \ from the path
' s/[]/[]//G ' Remove all spaces and replace with T a B key
' S/^ []//G ' Delete all t a b keys at the beginning of the line
' s/[] *//G ' Remove all t a b keys
If you use S e d to filter the file, it is best to divide the problem into steps, step-wise, and edge-test results.
experience tells us that this is the most effective way to perform a complex task.
                                                        

Common commands and syntax for Grep,awk and SED

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.