Linux command -grep, sed, awk

grep


(global search regular expression [RE] and print out the line)
regular expression search and a global print lines

  • Find the line that contains the string "text" in the file
grep text local_file
grep "text" local_file #另一种方式
grep "text" local_file1 local_file2 ...  #查找多个文件
  • Find in Files not line containing the string "text" of
grep -v "text" local_file
  • Ignore case
grep -i "TeXt" local_file
grep -y "TeXt" local_file
  • No error messages , commonly used in the script file
grep -s "text" local_file
  • Print out only the matched character string
grep -o "text" local_file
  • Statistics document the number of lines that contain the string you want to find
grep -c "text" local_file
  • Does not output information , failure command runs successfully returns 0 returns non-zero, for determining
grep -q "text" local_file
  • Matching a plurality of strings, or equivalent logic
grep -e "text1" -e "text2" local_file
  • Recursive search for files multi-level directory
grep -r "text" . #在当前目录下进行查找
  • Output matching the search string and the line before the line
grep "text" -B 3 local_file #输出之前的3行
  • Output matching the search string and line after line
grep "text" -A 3 local_file #输出之后的3行

and


Stream editor for editing one or more files, the file operation is repeated simplified. Operating in the buffer zone, unless otherwise specified, the file itself does not modify the content.

-i

To modify the file itself

-q

  • After printing out the exit line 2sed
sed '2q' local_file

Seek

  • Find rows 2-5
sed '2,5p' local_file
sed -n '2,5p' local_file #并打印行号
  • Find the line in the row that contains the string "text" and contains the string "file" line range
sed '/text/,/file/p' local_file
  • Find all the way to the line between the string "text" at the beginning of the line starting at line 2
sed '2,/^text/p' local_file

Add to

  • Add the string "text" in the second line after the line
sed '2a text' local_file
  • Add the string "text" in the second line of the previous line
sed '2i text' local_file
  • In front of each word plus the character "a":
sed 's/\w\+/a&/g'  # \w\+匹配每一个单词 &对应之前匹配的每一个单词

replace

  • Replacement string fileisfiles
sed 's/file/files/g' local_file #打印到控制台,不修改文件
sed 's:file:file:g' local_file # /标记可以使用其他符号代替
sed -i 's/file/files/g' local_file #修改文件本身内容,不打印到控制台
  • Replace 2-5 behavior string "text"
sed '2,5c text' local_file

delete

  • Line 2-5 in the deleted file
sed '/2,5/d' local_file
  • Delete the beginning of string "text" line
sed '/^text.*//g' local_file
sed '/^text/'d local_file
  • Delete the last line
sed '$d' local_file
  • Delete blank lines
sed '/^$/d' local_file

awk


  • Second and third print column data for each row
awk '{print $2,$3}' local_file

Guess you like

Origin www.cnblogs.com/cbkj-xd/p/12079856.html