Common basic usage features of Find grep sed awk under LINUX

Source: Internet
Author: User
Tags character classes

Common basic usage features of Find grep sed awk under LINUX

Just to make yourself a higher standard, this is the beginning of a 6-month small white advanced road!

Learning Linux Basic commands these days is a bit of a crime, especially for files looking for this piece. All I want to put some of the practical parameters I think commonly used to summarize, the big God do not spray ha ...

Please advise, the little woman here thanked.

Thefind: command is used to find the file specified in the file system with the following command format:

1. Find Pathname-options [-print-exec-ok ...]

(1) Parameters of the Find command:

The directory path that the Pathname:find command looks for.

The-print:find command outputs the matched file to standard output.

The-exec:find command executes the shell command given by the parameter to the matching file. The form of the corresponding command is ' command ' {};, note {} and; Space between the.

-ok: The same as-exec, except that the shell command given by the parameter is executed in a more secure mode, prompting the user to determine whether to execute before executing each command.

(2) Find rule:

-perm #按照权限查找

-name #按照文件名来查找 (-name supports wildcard characters or ")

-user #按照文件属主来查看文件

-group #按照文件的所属组来查看文件

-exec #查找后执行命令的时候不询问用户, direct execution

-type #根据文件类型查找文件 (plain file-F; directory file-D; link file-l; block device file-B; character device file-C; pipe file-p;socket file-s)

eg.

Find. -name 1.txt #在当前目录以及子目录下查找1. txt

Find. -name "1.*"

Find/tmp-name 1.txt #在/tmp directory to find 1.txt

Find/tmp-type s

find/bin/-perm 4755 | xargs ls-al (Xargs used to convert the output of the previous command to the parameters of the latter command)

find/bin/-exec 4755 | xargs ls-l {} \

Second, grep: can match the text with regular expressions and print the matching lines


-C #只输出匹配行的次数

-R #查询目录及其子目录

-I #不区分大小写 (only for single characters)

-N #显示匹配出的载文件中是第几行

-V #显示不包含匹配文本的所有行

-N #显示匹配出的载文件中是第几行

--color=auto #找到的关键字加上颜色显示

-A #显示匹配行以及下面3行 grep-a 3 root/etc/passwd

-B #显示匹配行以及上面3行 grep-b 3 root/etc/passwd

-C #显示匹配行以及上下各3行 grep-c 3 root/etc/passwd

The string file that grep looks for

Grep-r Find the String directory


(1)/etc/passwd, highlight the row where Root appears, and display the line numbers of these lines in/etc/passwd

# grep-n ROOT/ETC/PASSWD

(2) Finding a directory recursively based on file contents

# grep ' ettercap ' # search for files with 'ettercap' line in the current directory

# grep-r ' ettercap ' * # #在当前目录及其子目录下搜索' energywise ' line of files

# grep-l-R ' ettercap ' * #在当前目录及其子目录下搜索 ' ettercap ' file, but does not display matching lines, only matching files are displayed

(3) Reverse selection of character classes [^]: If you want to search for lines with CC, but do not want oo preceded by G, the following grep -n' [^a-z]oo ' 2. TXT

(4) Grep-r cams/root/

(5) Search for files starting with Root PS aus | grep VI--color

(6) get a line with a number, as follows grep-n' [0-9] ' 2.txt

(7) find blank line grep-n ' ^$ ' 2.txt

Third, sed: mainly in the conduct of the processing units, the data will be added to the selection and other ...

sed parameters [-NEFR] action [function] File

1. Options and Parameters:

-N: Use Quiet mode. In the general SED usage, all data from stdin is generally listed on the terminal. But if you add the-n parameter, it is only the line (or action) that the SED is specifically handling to be listed.

-E: Direct in the command mode of the cloud line, SED action editing;

-F: The action of the SED is written directly in a file, and the. N filename can run the SED action within filename;

-r:sed's actions support the grammar that extends the regular expression of the hair. (The default is the French law of the basic regular notation);

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

2, Action Description: [n1[, N2]] function

N1,N2: Not necessarily hi exists, generally represents "select the number of lines of action", for example, if my action is required between 10 to 20 rows, then "10,20[function"

function

A: New, new content appears on the next line (currently the next line);

C: Replace, C can be followed by a string, these strings can replace the line between N1,N2

D: Delete, usually do not pick up anything behind D;

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

P: Print, print out a selected data. Normally p will run with parameter sed-n

S: Replace, usually this action can be paired with regular expressions

eg.

1) Add/Remove NL/ETC/PASSWD to behavioral units | Sed ' 2,5d '

2) go straight to delete the second line nl/etc/passwd | Sed ' 2d '

3) to delete 3rd to last row nl/etc/passed | Sed ' 3, $d '

4) Add the words "drink tea" after the second line (that is, add the third line) nl/etc/passwd | Sed ' 2a drink tea '

5) Add more than two lines of content "drink tea ..." with "Drink beer?" NL/ETC/PASSWD | Sed ' 2a drink tea......\>drink beer? '

6) Multi-point editing, delete/etc/passwd third line to the end of the data, and the bash to replace the Blueshell

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

awk: To put it simply, awk reads the file line-by-row, using a space as the default delimiter (-f) to slice each row, and the cut part is processed in various analytical processes.

The first is the use of the-F delimiter that confuses me for a long time, in fact, something like cat:

For example, files that manipulate data:

Zhc-123|zhang

Hongchangfirst-99|zhang

Hongchang-100|zhang

If we awk-f '-' {print $;} ' data, it will print out

Zhc

Hongzhangfirst

Hongzhang

The delimiter default is a space,

Variable name meaning

ARGC Command Line arguments

ARGV command-line array of elements

FileName current Input file name

FNR the record number in the current file

FS input domain delimiter, default is a space

RS Input Record delimiter

NF number of fields in the current record

NR Record Count so far

OFS output field delimiter

ORS Output Record delimiter

Eg.last-n 5 (remove the first five elements only)

Last-n 5 | awk ' {print '} ' shows only the 5 most recently logged accounts

$ A represents all fields representing the first domain $n representing n fields

(1) command line mode-(highlights) awk [-F field-separator] ' commands ' input-file (s)

Where commands is the true awk command, [-F domain delimiter] is optional. Input-file (s) is pending file

In awk, each line in a file, separated by a domain delimiter, is called a domain. In general, the default field delimiter is a space without specifying the-f domain delimiter.

(2) Shell script mode

Insert all the awk commands into a file and make the awk program executable, then awk shows the interpreter as the first line of the script, typically called by typing the script name. Equivalent to the first line of the shell script: #!/bin/bash====>#!/bin/awk

(3) Insert all SWK commands into a separate file and then invoke: Awk-f awk-script-file input-file (s)


This article is from the "11666364" blog, please be sure to keep this source http://11676364.blog.51cto.com/11666364/1829003

Common basic usage features of Find grep sed awk under LINUX

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.