Find grep sed awk detailed

Source: Internet
Author: User

Find: Finds the file specified in the file system. You can find by file name (-name) permission (-perm) attribution.

Find the path expression to locate the file

* Wildcard characters can be added anywhere in the file name

Common examples (refer to find--help for details):

Find. -name 1.txt Find files in current directory and subdirectories 1.txt

Find/tmp-name 1.txt Find files in/tmp directory and subdirectories 1.txt

find/bin/-perm 4755 | The function of the Xargs ls-al pipe symbol is to use the previous output as the following parameter to perform the subsequent part.

Find/tmp-name "1.txt" | -exec ls-l

Find/tmp-name ' *.txt '-exec ls-l {} \;

grep: A powerful text-search tool that uses regular expressions to search for text and print matching lines.

grep [command options] to find the matching pattern [filename to find]

-C Displays the number of matches per line

-I is case insensitive

-N Displays travel numbers and lines

-V does not match to the line output to

-R Recursive Lookup

-a displays matching lines and lines below 3 lines grep-a 3 root/etc/passwd

-B displays matching rows and rows above 3 lines Grep-b 3 root/etc/passwd

-C Displays the matching line and the following and above 3 lines grep-c 3 root/etc/passwd

grep cams/root/1.txt Find Cams string in 1.txt file

Grep-r Cams/root/cams finds the Cams string in all files under the/root/cams directory and its subdirectories, and prints the matching lines.

Grep-a 1 ' root '/etc/passwd
In addition to being able to find files, grep is able to redirect any output stream to grep for lookups:

PS aux | grep Ora finds all process information in the process name that contains Ora

sed : is a good file processing tool, itself is a pipeline command , mainly in line for processing, you can replace the data, delete, add, select and other specific work.

sed parameters [-nefr] Action file

Parameters:

-N: Quiet mode. Only the line (or action) that has been specially processed by SED will be listed.

-F: Write the SED action directly in a file,-f filename, you can run the SED action within filename.

-R: The default is the basic formal French notation

-I: Directly modifies the contents of the read file, not the output to the terminal.

Action:

A: New, a can be followed by a string, and these strings will appear on the next line.

C: Replace, C can be followed by a string.

D: Delete, the back of D does not pick up anything.

I: Insert, I can be followed by strings, these strings will appear on a new line (the current line).

P: printing, print a selected data, general and sed-n combination

S: Replace, can be replaced directly, usually with formal notation. Example: 1,20s/old/new/g replace the first line to the 20th line with new, and g replace all

Common example, you can try the following statement specifically what it means:

The action behind the SED, only with single quotes

nl/etc/passwd | Sed ' 2,5d '
nl/etc/passwd | Sed ' 2d '

nl/etc/passwd | Sed ' 2, $d '
nl/etc/passwd | Sed ' 2a drink tea '

nl/etc/passwd | Sed ' 2i drink tea '

nl/etc/passwd | Sed ' 2a Drink tea or \
Drink beer? '

nl/etc/passwd | Sed ' 2,5c NO 2-5 number '
nl/etc/passwd | Sed-n ' 5,7p '
nl/etc/passwd | Sed-n '/root/p '

nl/etc/passwd | Sed '/root/d '
nl/etc/passwd | Sed-n '/root/{s/bash/blueshell/;p} '

nl/etc/passwd | Sed-n '/bash/{s/bash/bashshell/;p; q} '
Sed ' s/to be replaced by string/new strings/g '

/sbin/ifconfig eth0 | grep ' inet addr ' |sed ' s/^.*addr://g ' | Sed ' s/bcast.*$//g '
/sbin/ifconfig eth0 | grep ' inet addr ' |sed ' s/^.*addr://g '

nl/etc/passwd | Sed-e ' 3, $d '-e ' s/bash/blueshell/'

awk: is a powerful text analysis tool. Typically awk is handled as a unit of the file's behavior, and awk handles the text by executing the corresponding command for each line of the file it receives.

There are three ways to invoke awk:

1. Command line mode (highlights)

2.shell Script Mode

3. Insert all the awk commands into a separate file and then call

Specific examples:

1, last-n 5 | awk ' {print '} ' prints the first column of the preceding five rows (all columns, $ = nth column)

2, CAT/ETC/PASSWD | Awk-f ': ' {print ' \ t ' $7} '

Displays only the first and seventh columns of the/etc/passwd, separated by the TAB key. -f Specifies the domain delimiter as ":".

3, CAT/ETC/PASSWD | Awk-f ': ' BEGIN {print ' Name,shell '} {print $ ', ' $7} END {print ' Blue,/bin/nosh '} '

Only the/etc/passwd account and the shell of the account are displayed, and the account is separated from the shell with a comma, and all rows add the column name Name,shell, adding "Blue,/bin/nosh" to the last line.

4, Awk-f: '/root/{print $7} '/etc/passwd

Search all lines that have the root keyword/etc/passwd and display the corresponding shell.

The following are AWK programming, which can be custom variables, conditional statements (from the C language), loop statements, arrays.


5, awk ' {count++;p rint;} End{print "User Count is", count} '/etc/passwd

Statistics on the number of/ETC/PASSWD accounts
6. awk ' BEGIN {count=0;print ' [Start]user count is ', count} {Count=count+1;print $;} End {print "[end] User Count is", count} '/etc/passwd

This is also the count of/etc/passwd accounts.

It's so much the usual! Write a summary to this, later discovered the new world, come again to add.

Here's the difference between SED and awk:

Awk is a programming language that has a strong ability to handle document data, and awk is adept at extracting data from formatted messages or a large text.

Sed is a streamlined, non-interactive editor that performs the same editing tasks as compiling VI.

Summarize:

1, if the file is formatted, that is, the delimiter is divided into multiple domains, using awk preferentially;

2. awk is suitable for column (domain) operations, and SED is suitable for line-by-row operation.

3. Awk is suitable for extracting files, and sed is suitable for editing files.

Note: the sed and awk edits to the file do not change the file itself, only the output is changed.




Find grep sed awk detailed

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.