Linux Common Text filter commands (find grep WC awk sed sort uniq split) __linux

Source: Internet
Author: User
Tags disk usage

All support Regular expression
1. Find
-name Search by name
-perm Based on Permissions lookup
-ctime–n +m lookup based on creation time,-n days, +n n days ago
-mtime–n +n Last Modified time
-exec command {} \; Execute command for each record
-ok command {}\; ibid.
Example: Find. –name "*.log" –exec ls–al {} \;
Find/var-name "*.log"-mtime +10-exec ls-l {} \;

2. grep
Grep–c "SDF" *.txt only returns a matching number of rows
Grep–n Output Line number
grep "2010-5-1[0-9]" myfile number 10th to 19th.
grep "^[^123]" myfile not 1,2,3 big head.
grep "4\{2\}" MyFile 2 consecutive 4
grep "4\{2,\}" myfile at least 2 consecutive 4
grep "4\{2,5\}" MyFile 2 to 5 consecutive 4
grep "^$" myfile Blank line
grep "\^" myfile lookup ^ symbol, using \ filter to turn the meaning

3. WC
WC is used to calculate the number of characters, bytes, lines, etc.
Wc–l <myfile Returns the number of myfile rows

4. awk (very good very strong)
awk ' {print $} ' myfile displays all fields of MyFile ($), with a space separator (default)
Awk–f "Asdf" ' {print} ' myfile display 1 fields, separator asdf
Awk-f ":" ' BEGIN {print ' hire_date\n-------------------'}{print $} ' messages |head
The begin {Command} End{command} structure in awk, specifying the operation of the head and tail, different from the general begin END block structure

Special meta characters in awk: +,? + Match all,? Match a single character
Match operator: ~,!~//~ match,!~ mismatch

Head messages |awk ' $ ~/21:59/' matches 21:59 rows, which is equivalent to the grep feature, but awk can more precisely control the specific domain.
Head messages |awk ' $!~/21:59/' does not match 21:59 rows (grep-i)

[Mysql@node1 ~]$ head Messages |awk ' {if ($3== ' 21:59:48 ') print $} '
May 21:59:48 Node1 Dhclient:dhcprequest in eth1 to Port 67
May 21:59:48 Node1 Dhclient:dhcpack from
May ten 21:59:48 Node1 Dhclient:bound to in 843 seconds.
Awk's own control flow structure, which seems to be closer to c than the general shell syntax

5. Sed
Sed is an online editor that handles a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer, known as pattern spaces, followed by the SED command to handle the contents of the buffer, and the contents of the buffer are sent to the screen after processing is complete. The next line is processed so that it repeats until the end of the file. The contents of the file have not changed.

-N is a printed line that does not match, and the default is to print all
-E followed by the script until the new line or the-e end is encountered. If-E is not given, the first command does not end until the new row
-F followed by script file

Basic editing commands:
P Print Matching rows
= Show line number
A\ add information After a row is positioned (new line)
I\ inserts information before the row is positioned (new line)
D Delete the Anchor line
C\ Replace the positioned row contents, note that the entire line
S/re/string replaces the regular expression re with a string.
G means full replacement in line.

Note that the contents of pattern are regular expressions
Sed ' 2p ' myfile print all Lines
Sed–n ' 2p ' myfile promised the second line,-------print unmatched rows, by default, print all
Sed–n ' 1,7p ' myfile print 1 to 7 lines (1,$ is 1 to the last line)
Sed–n '/fuyar/p ' myfile print matching Fuyar rows, pattern matching method
Sed–n ' 2,/fuyar/p ' myfile from the second line to the end of the match to the Fuyar. The combination of line number mode and pattern matching method
=: Print the current line number.
Sed–n '/^$/= ' myfile display blank line number
Sed–n–e '/^$/= ' –e '/^$/p ' myfile displays a blank line number and prints
Sed-n ' s/param/& hellow/p ' added param after hellow
Sed-n ' S/param/hellow &/p ' at Param before adding hellow

6. Sort
-C Check whether the file is sorted
-U-Unique meaning, after sorting, duplicate records show only one
-R reverse, reverse sequence
-N Numeric sort
-kn is sorted by nth field, equivalent to + N-1–n, now recommended with K

SORT-T:-K3 Messages | Head equivalent to SORT-T: +2-3 messages | Head

7. Uniq
Remove or disallow duplicate rows from one text, where duplicate lines refer to adjacent. Can be used in conjunction with sort.
-C Displays the number of rows repeated for each record
-U displays only the rows that are not duplicates, that is, those rows with-C is 1
-D displays only duplicate rows, but only 1 times per bar

8. Split
Split file, split file as: Prefix[aa-zz], e.g. Yyaa,yyab....yyzz
General format: Split [options] infile Outfile_prefix
-B-N split with size n (k,m)
-L-N the number of rows per file (n)
-N with-l n

Several commands are used in combination to write some simple shell scripts.
Example: Monitor disk usage, check every minute, and send an email alert to root if it is nearly full (using more than 90%).
[Root@node1 ~]# Cat
#space_oversee. Sh by Fuyar (417226209).
#to oversee the usage of all disks.
#oversee per minute.

While [1-lt 2]
For DISK in ' df | awk ' {print $} ' | Sed-n ' 2,/$/p '
Used= ' DF | grep "$DISK" |awk ' {print $} ' |sed-n ' s/%//p '
If [$USED-ge 90]
Then echo "' Date ': $DISK is nearly full: ${used}%." | Mail root
Sleep 60

This script can be run in the background

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: 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.