Grep awk sed command tutorial

  
 

1. Double quotes and single quotes When entering string arguments in the g r e p command, it is best to enclose them in double quotes. For example: “m y s t r i n g”. There are two reasons for this, one is to prevent misunderstanding as a shell command, and the other is to find a string of multiple words, for example: "jet plane”, if you do not enclose it in double quotes, then the word The plane will be mistaken for a file, and the result of the query will return an error message that the file does not exist. Double quotes should also be used when calling variables, such as: g r e p“$ M Y VA R” filename, if not, no result will be returned. Single quotes should be used when calling pattern matching. [root@mypc ]# echo `grep 123 111.txt` (#Note is anti-single quote) 2. The commonly used g r e p options are: -c Output only the count of matching lines. -i is not case sensitive (only for single characters). -h Does not display the file name when querying multiple files. -l When querying multiple files, only the file name containing the matching characters is output. -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 the string in all .doc files in the current directory<quo;sor t”)

$ grep "sort it" * (#or query the word in all files <;sort it”) The next example is to query in a single file. 4. The line matches $grep -c "48" data. f$ 4 (#grep returns the number 4, meaning there are 4 lines containing the string “4 8”.) $ grep "48" data.f (#displays 4 lines of text containing "4 8” string") 5. Display the number of rows and rows that match the matching pattern: [root@mypc oid2000]# grep -n 1234 111.txt 1:12343:1234ab6. Exact match [root@mypc oid2000]# grep "1234\\>" 111.txt 12347. Query blank lines to query for lines that begin or end with a condition. Use ^ and $ together to query empty lines. Use the -n parameter to display the actual number of lines [root@mypc oid2000]# grep -n "^$" 111.txt (return result 2: #describe the second line is blank) [root@mypc oid2000]# grep - n "^abc" 111.txt (#Query the line starting with abc)[root@mypc oid2000]# grep -n "abc$" 111.txt (#Query the line ending with abc) 8. Match special Character, query for characters with special meaning, such as $ . ' " * [] ^

Copyright © Windows knowledge All Rights Reserved