I believe that when you use Linux, you will always encounter situations where you want to extract some of the information you need, such as the following four scenarios:
1. Find the IPV4 address of eno16777728 in ifconfig command result
2. Find out the maximum percentage value of partition space utilization
3. Find out the permissions of/tmp and display them digitally
At this point, we can certainly view the commands, but we also need to filter out unwanted information through our eyes. How can you make it easier for you to see the information you want to see? Today's text processing commands are available to meet our simple needs.
WC This WC is not WC, where WC is a shorthand for word count
Wc-print newline, Word, and byte counts for each file
its expression format: WC [OPTION] ... [FILE] ...
Common options:
-l:lines shows only the number of rows
-w:words Show only the total number of words
-c:bytes only displays the total number of bytes in this content
The following example shows the function of WC in detail.
Create a/test directory to create a/test/wc.txt file
[email protected] test]# cat > 1hello mehello my Boy
Use Cat to enter some characters in Wc.txt
[[email protected] test]# WC wc.txt 2 5 x Wc.txt The first 2 represents the number of rows the second 5 represents the word count the third 22 represents the total number of bytes of the content [email protected] test]# wc-l WC . txt 2 wc.txt[[email protected] test]# wc-c wc.txt (wc.txt[[email protected] test]# wc-w wc.txt 5 wc.txt
Cut
Cut-remove sections from all line of files
expression Format : Cut OPTION ... [FILE] ...
Common options:
-d<char>: Delimiter with the specified character
-F # (single field) |#-# (multiple fields in a row) |#,...,# (discrete multiple fields)
-C cut by character
--output-delimiter=string specifying the output delimiter
Experiment with/etc/passwd file as an object
1, take the user name and user uid and specify the output delimiter is #
[Email protected] test]# tail-5/etc/passwdlaowang:x:4322:4322::/home/laowang:/bin/bashu1:x:4323:4323:uuu:/home/ u1:/bin/cshu2:x:4324:4324::/home/u2:/bin/bashu3:x:4325:4325::/home/u3:/bin/bashu4:x:4326:4326::/home/u4:/sbin/ Nologin
Through the above, we can determine what we need in the first section and the third section, the colleague separator is ":"
[Email protected] test]# cut-d:-F 1,3/etc/passwd--output-delimiter= #root #0bin#1daemon#2adm#3.
2. View the last line of the/etc/passwd file, and intercept the tenth character starting at the 5th character.
[Email protected] test]# TAIL-1/etc/passwd | Cut-c 5-10:4326:
Sort sort
Sort-sort lines of text files
Expression format: Sort [OPTION] ... [FILE] ...
Common options:
-T CHAR: Specify delimiter
-K #: field for sort comparison
-N: Sorting based on numeric size
-R: Reverse order
-F: Ignore character case
-U: Duplicate content retains only one row
or test it with a/etc/passwd object.
Displays the user with the largest UID and their default shell
[Email protected] test]# sort-t:-K 3-n/etc/passwd root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/ Nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologin...basher:x:4329:4329::/home /basher:/bin/bashnologin:x:4330:4330::/home/nologin:/sbin/nologinnfsnobody:x:65534:65534:anonymous NFS User:/var /lib/nfs:/sbin/nologin
Show success, but the results are not intuitive, we are using the cut command above to further process the results
[Email protected] test]# sort-t:-K 3-n/etc/passwd | Tail-1 | Cut-d:-F 1,7nfsnobody:/sbin/nologin
Uniq report or remove duplicate rows
Uniq-report or omit repeated lines
Expression format: Uniq [OPTION] ... [INPUT [OUTPUT]]
Common options:
-C: Shows the number of repetitions per row
-U: Show only rows that have never been repeated
-D: Show only rows that have been repeated
To demonstrate the convenience of the Uniq command, we create a file with duplicate rows/test/uniq.txt
[email protected] test]# cat uniq.txt qqqqqqqqqqqqdfsdfaabbbbccq[[email protected] test]# uniq uniq.txt Qqqqdfsdfaabbccq
The Uniq file defaults to hide duplicate content.
[[email protected] test]# uniq-c uniq.txt 3 qqqq 1 dfsdf 1 AA 2 BB 1 cc 1 Q 1 q[[emai L protected] test]# uniq-u uniq.txt dfsdfaaccq[[email protected] test]# uniq-d uniq.txt QQQQBB
diff similarities and differences in comparing files by line
Diff-compare Files line by line
Diff [OPTION] ... FILES
Common options:
-U: Using the unified mechanism, which displays the context of the row to be modified, the default 3 rows
[[email protected] test]# cat diff1 diff2abcdabcdeabcdbcdabcdebc[[email protected] test]# diff diff1 diff2 1c1< abcd- --bcd3c3< ABCD---> Bc[[email protected] test]# diff-u diff1 diff2---diff1 2016-08-05 19:46:36.985538120 +0800+++ diff2 2016-08-05 19:46:54.951836769 +0800@@ -1,3 +1,3 @@-abcd+bcd ABCDE-ABCD+BC
Patch Patch the file
Basic concepts patch-apply changes to files
Expression format patch [-blnr][-c|-e|-n][-d dir][-d define][-i Patchfile]
[-O outfile] [-P num] [-R Rejectfile] [File]
Patch [OPTION]-l/path/path_file/path/oldfile
Patch/path/oldfile </path/path_file
Commonly used simple Text Processing command is complete, the following to use the command described in this article to solve the four questions raised at the beginning
1. Find the IPV4 address of eno16777728 in ifconfig command result
Ifconfig | Tr-cs ' [:d igit:]. ' ': ' | Cut-d:-F 510.1.253.79
The above is divided into 3 steps
1) First the content of the ifconfig as the basic input of TR; 2) Replace all non-digital content in the first step with ":" and compress; 3) look at the required IP in the paragraph and then cut with the Cut command
2. Find out the maximum percentage value of partition space utilization
View partition Space Command for DF
[[email protected] test]# DF | Tr-s ': ' | Cut-d:-F 5| tr-d '% ' |sort-n|tail-129
To achieve the above content needs
1) Use DF to list the contents of the partition space usage,
2) then use TR to replace the space with: and to compress,
3) Then use cut to cut the rate of use of the column out,
4) then use TR to remove the%,
5) Sort by numeric value after using sort
6) finally use tail to take the maximum value of the last line.
3. Find out the permissions of/tmp and display them digitally
Check/TMP permission can use stat, it can automatically display the value corresponding to its permission, the rest only need us to remove the number from the content.
[Email protected] test]# stat/tmp/| Tr-cs ' [:d igit:] ': ' | Cut-d:-F 91777
1) Show permission content first
2) Replace all non-digits in the content with ":" and compress
3) Number of the corresponding permission number after the first segment of the cut
This article is from "Zhang Fan-it's fantasy drifting" blog, please be sure to keep this source http://chawan.blog.51cto.com/9179874/1834875
Linux Basic Text Processing commands (Wc,cut,sort,uniq,diff,patch)