Linux text processing commands (wc, cut, sort, uniq, diff, patch)

Source: Internet
Author: User
Tags directory create

Linux text processing commands (wc, cut, sort, uniq, diff, patch)

I believe that when using Linux, you will always encounter situations where you want to extract information you need, such as the following four situations:
1. Find the IPv4 address of eno16777728 in the ifconfig command result.
2. Find the maximum percentage of partition space usage
3. Check the/tmp permission and display it in numbers.
At this time, we can also view the command, but we also need to filter unwanted information through the eyes, it is much effort. How to make it easier for you to see what you want to see? Today's text processing commands can meet our simple needs.

Wc this wc is not the WC, where wc is short for word count

Wc-print newline, word, and byte counts for each file
The format is wc [OPTION]... [FILE]...
Common options:
-L: lines only displays the number of rows
-W: words: only display the total number of words
-C: bytes only displays the total number of bytes of the content

The following example shows the function of wc.
Create/test directory create/test/wc.txt File
[Root @ localhost test] # cat> 1
Hello me
Hello my boy

Use catto input some characters in wc.txt

[Root @ localhost test] # wc wc.txt
2 5 22 wc.txt the first one 2 represents the number of rows the second 5 represents the number of words the third 22 represents the total number of bytes of the content
[Root @ localhost test] # wc-l wc.txt
2 wc.txt
[Root @ localhost test] # wc-c wc.txt
22 wc.txt
[Root @ localhost test] # wc-w wc.txt
5 wc.txt

Cut
Cut-remove sections from each line of files
Expression format: cut OPTION... [FILE]...
Common options:
-D <char>: Use the specified character as the separator.
-F # (single field) | #-# (multiple consecutive fields) | #,..., # (discretization of multiple fields)
-C cut by character
-- Output-delimiter = STRING specifies the output Separator
Experiment with the/etc/passwd file
1. Take the user name and user UID and specify the output separator #

[Root @ localhost test] # tail-5/etc/passwd
Laowang: x: 4322: 4322:/home/laowang:/bin/bash
U1: x: 4323: 4323: UUU:/home/u1:/bin/csh
U2: x: 4324: 4324:/home/u2:/bin/bash
U3: x: 4325: 4325:/home/u3:/bin/bash
U4: x: 4326: 4326:/home/u4:/sbin/nologin

Through the above content, we can determine what we need in the first section and the third section, the colleague separator is ":"
[Root @ localhost test] # cut-d:-f 1, 3/etc/passwd -- output-delimiter = #
Root #0
Bin #1
Daemon #2
Adm #3
..

2. view the last line of the/etc/passwd file, and truncate it from 5th characters to the tenth character.
[Root @ localhost test] # tail-1/etc/passwd | cut-c 5-10
: 4326:

Sort sorting

Sort-sort lines of text files
Format: sort [OPTION]... [FILE]...
Common options:
-T CHAR: Specifies the delimiter
-K #: fields used for sorting and Comparison
-N: sorting based on the value size
-R: sort in reverse order
-F: case-insensitive characters
-U: Repeat only one row
Or test with/etc/passwd as the object
Displays the largest UID user and its default shell
[Root @ localhost test] # sort-t:-k 3-n/etc/passwd
Root: x: 0: 0: root:/bin/bash
Bin: x: 1: 1: bin:/sbin/nologin
Daemon: x: 2: 2: daemon:/sbin/nologin
Adm: x: 3: 4: adm:/var/adm:/sbin/nologin
...
Basher: x: 4329: 4329:/home/basher:/bin/bash
Nologin: x: 4330: 4330:/home/nologin:/sbin/nologin
Nfsnobody: x: 65534: 65534: Anonymous NFS User:/var/lib/nfs:/sbin/nologin

The result is displayed successfully, but the result is not intuitive. We use the cut command above to further process the result.

[Root @ localhost test] # sort-t:-k 3-n/etc/passwd | tail-1 | cut-d:-f, 7
Nfsnobody:/sbin/nologin

Uniq report or remove duplicate rows
Uniq-report or omit repeated lines
Format: uniq [OPTION]... [INPUT [OUTPUT]
Common options:
-C: displays the number of duplicates in each row.
-U: only show rows that have not been repeated
-D: only show repeated rows
To demonstrate the convenience of the uniq command, we create a file with duplicate lines/test/uniq.txt
[Root @ localhost test] # cat uniq.txt
Qqqq
Qqqq
Qqqq
Dfsdf
Aa
Bb
Bb
Cc
Q
[Root @ localhost test] # uniq uniq.txt
Qqqq
Dfsdf
Aa
Bb
Cc
Q

By default, uniq files hide duplicate content.
[Root @ localhost test] # uniq-c uniq.txt
3 qqqq
1 dfsdf
1 aa
2 bb
1 cc
1 q
1 q
[Root @ localhost test] # uniq-u uniq.txt
Dfsdf
Aa
Cc
Q
[Root @ localhost test] # uniq-d uniq.txt
Qqqq
Bb

--------------------------------------------------------------------------------
Comparison of diff lines by line

Diff-compare files line by line
Diff [OPTION]... FILES
Common options:

-U: Use the uniied mechanism to display the context of the row to be modified. The default value is 3 rows.
[Root @ localhost test] # cat diff1 diff2
Abcd
Abcde
Abcd
Bcd
Abcde
Bc
[Root @ localhost test] # diff diff1 diff2
1c1
<Abcd
---
> Bcd
3c3
<Abcd
---
> Bc
[Root @ localhost test] # diff-u diff1 diff2
--- Diff1 19:46:36. 985538120 + 0800
++ Diff2 19:46:54. 951836769 + 0800
@-1, 3 + 1, 3 @@
-Abcd
+ Bcd
Abcde
-Abcd
+ Bc

Patch to 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

--------------------------------------------------------------------------------
The common simple text processing commands are introduced. The following commands are used to solve the four problems raised at the beginning.

1. Find the IPv4 address of eno16777728 in the ifconfig command result.
Ifconfig | tr-cs '[: digit:]. '':' | cut-d:-f 5
10.1.253.79

The preceding steps are divided into three parts:
1) Use the ifconfig content as the basic input content of tr; 2) replace all non-numeric content in step 1 with ":" And compress it; 3) check the required IP address segment and use the cut command to cut it.

2. Find the maximum percentage of partition space usage
View the partition Space Command as df
[Root @ localhost test] # df | tr-s ''': '| cut-d:-f 5 | tr-d' % '| sort-n | tail-1
29

To implement the above content
1) use df to list the usage of the partition space,
2) use tr to replace the space with: and compress the space,
3) then, use cut to cut out the columns for utilization,
4) use tr to remove %,
5) Use sort to sort by numerical value.
6) use tail to obtain the maximum value of the last row.

3. Check the/tmp permission and display it in numbers.
You can use stat to view/tmp permissions. It can automatically display the values corresponding to the permissions. You only need to extract the numbers from the content.
[Root @ localhost test] # stat/tmp/| tr-cs '[: digit:] '':' | cut-d:-f 9
1777

1) display the permission content first
2) replace all non-numbers in the content with ":" And compress it.
3) Calculate the corresponding permission number and cut it after the nth segment.

This article permanently updates the link address:

Related Article

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.