Linux Shell Script Raiders notes
Fourth Chapter: Efficient Text Processing1, the IP address of the regular expression: [0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\. [0-9] {1,3}2, grep usage//recursive retrieval of text in a multilevel directory [[email protected] program_test]# grep "yang"./-rn./test.txt:6:laoyang
./right.txt:1:1 Yang Man
Ignore case matching [[email protected] program_test]# echo Hello World | Grep-i "HELLO"
Hello world//recursively searches all. C and. cpp files [[email protected] program_test]# grep "main ()". -R--include *. {C,cpp}
./hello.c:int Main ()
Sin.c:int Main ()
Hello.cpp:int Main ()
Several lines after matching a result [[email protected] program_test]# echo-e "A\NB\NC\NA\NB\NC" | grep a-a 1
A
B
--
A
B
3, Cut command Cut, the text by the column to cut the gadget. -D delimiter; -F column to extract [[email protected] program_test]# cut-d ":"-f5--complement Passwd_yang
Root:x:0:0:/root:/bin/bash
Bin:x:1:1:/bin:/sbin/nologin
[Email protected] program_test]# cut-c1-5 Passwd_yang
Root
Bin:x
Daemo
Adm:x
Count the word frequency in a specific file
[email protected] program_test]# cat word_freq.sh #!/bin/bashif [$#-ne 1];thenecho "Usage: $ filename" Exit-1fifilena Me=$1egrep-o "\b[[:alpha:]]+\b" $filename | awk ' {count[$0]++} END {printf ("%-14s%s\n", "word", "count"), for (Ind in count) {printf ("%-14s%d\n", Ind,count[ind]);}} '
4. The sed command (stream editor) applies text processing.//1. Replace, replace from 3rd [[email protected] program_test]# echo this thisthisthis | Sed ' s/this/this/3g '
This THISTHISTHIS//2. Delete blank line [[email protected] program_test]# sed '/^$/d ' choice.sh
3. Matched string tag &[[email protected] program_test]# echo This is an example | Sed ' s/\w\+/[&]/g '
[This] [IS] [An] [Example]
4. Replacement example. [email protected] program_test]# cat Sed_data.txt
ABC 111 this 9 file contains 111 numbers 0000
[email protected] program_test]# Cat Sed_data.txt |
sed ' s/\b[0-9]\{3\}\b/number3/g '
ABC NUMBER3 This 9 file contains NUMBER3 numbers 0000
5. awk tool for data flow, operation on columns and rows. 1), how awk is implemented [[email protected] program_test]# echo-e "Line1\nline2" | awk ' BEGIN {print ' begin...\n '} {print} END {print ' end...\n '} '
Begin ...
Line1
Line2
End ...
2), awk cumulative summation [[email protected] program_test]# SEQ 5 | awk ' BEGIN {sum=0; print ' summary: '} {print ' + '; sum+=$1;} END {print "= =" sum} ' summary:
1 +
2 +
3 +
4 +
5+
==15
3), awk sets the delimiter.//-f delimiter $NF the last field in a row [[email protected] program_test]# awk-f: ' {print ' \ t ' $NF} '/etc/passwd
Root/bin/bash
Bin/sbin/nologin
Daemon/sbin/nologin
4), print each letter in the file
[email protected] program_test]# cat read_each_word.sh cat hello.c | (While read Line;do #echo $line, for word in $line;d O#echo $word, for (i=0;i<${#word};i++) Doecho ${word:i:1};d Onedone Done)
5), Print 4–6 line content [[email protected] program_test]# SEQ 100 | awk ' nr==4, nr==6 '
4
5
6
6), awk to implement the function similar to TAC reverse order.
[[email protected] program_test]# SEQ 9 | awk ' {lifo[nr]=$0; Lno=nr} END {print "nr =" NR; for (; lno>-1;lno--) {print Lifo[lno];}} ' NR = 9987654321
Ming Yi World
Reprint please indicate source, original address: http://write.blog.csdn.net/postedit/42364823
If you feel this article is helpful, please click on the ' top ' support, your support is I insist on writing the most power, thank you!
Linux Shell Script Raiders Note Fourth: Efficient text processing