Linux string interception and processing commands cut, printf, awk, sed, sort, wc

Source: Internet
Author: User

1. Cut [Options] file name

-F column number #提取第几列 (delimited defaults think \ t)

-D delimiter #指定分隔符

Example: Cut-f 2 a.txt #截取文件a The second column of content (column number starting from 1)

Cut-f 2,4 A.txt #截取文件a The second and fourth columns of the. txt content

Cut-d ":"-F 1,3/etc/passwd #截取文件passwd文件的第1列和第三列 to: Splits the first and third columns

2. printf ' output type output format ' output content

Output Type:

%ns output string, n is a number, refers to output several characters

%ni the output integer. N is a number that refers to the output of several numbers

The number of%m.nf and decimal digits. For example:%8.2f represents the output of 8 digits, where 2 bits are decimals and 6 bits are integers

Output format:

\a Output Warning Sound

\b Output backspace key, i.e. backspace key

\f Elimination Screen

\ nthe line break

\ r Drawdown

\ t Horizontal tab

\v Vertical Tab

Example:

printf '%s%s%s\n ' 1 2 3 4 5 6 #表示每三个位一组输出, plus line break

printf '%s\t%s\t%s\t ' $ (cat a.txt) #表示以4列的形式输出

3. awk ' Condition 1{action 1} Condition 2{action 2} ... ' Filename

awk ' {printf $ "\ t" $6 "\ n"} ' A.txt #输出文件a the second and 6th columns of. txt

4. sed [options] ' [action] ' filename #说明: The action must be enclosed in quotation marks

Options:

The-N general sed command will output all data to the screen. If you join this selection, only the lines processed by the SED command will be output to the screen.

-e allows multiple sed command edits to input data

-I directly modifies the read data file with the result of the SED modification, rather than modifying the screen output

Action:

A\ Append, adding one or more rows after the current line. When you add multiple rows, the end of each line requires "\" to indicate that the data is not completed except for the last row.

C\ line substitution, replacing the original data row with the character after C, replacing multiple rows with a "\" at the end of each line to represent the data not ending.

I\ Insert, when inserting multiple rows at the current insertion, a "\" on the end of each line, except for the last line, means that the data is not completed.

d Delete, delete the specified row

P print, output the specified line

s string substitution, replacing another string with one string. The format is "line range s/old string/new string/g"

Sed can receive the output of a pipe character

Example:

Sed ' 2p ' a.txt #输出第2行后 and output all the content again

Sed-n ' 2p ' a.txt #只输出第二行

Sed ' 2,4d ' a.txt #删除第2到4行, only removes screen output and does not change the contents of the file itself

Sed ' 2a hello ' a.txt #在第二行插入一行 hello

Sed ' 2i hello ' \

word ' a.txt #在第二行前插入多行 hello one line of Word

Sed ' 2c no person ' a.txt #用no person to replace the second line

Sed ' 4s/99/55/g ' a.txt #把第4行的99替换为55

Sed-i ' 4s/99/55/g ' a.txt #把第4行的99替换为55, modified the original file instead of the screen output

Sed-i ' s/99/55/g ' a.txt #s前不加行号时表示替换整个文件中匹配的字符串

Sed-e ' s/liming//g;s/gao//g ' a.txt #-e means to allow multiple conditions to be executed, replace liming with empty, and replace the Gao with an empty

5. Sort [Options] file name #排序

Options:

-F ignores case

-N Sorts by numeric type, by default using string type sorting

-R Reverse Sort

-t Specifies the delimiter, the default delimiter is a tab

-K n[,m] is sorted by the specified field range. Starting with the nth field, the M field ends (default to end of line)

6. WC [option] File name #统计  

Options:

-L count rows only

-W only counts the number of words

-M only counts the number of characters

Linux string interception and processing commands cut, printf, awk, sed, sort, wc

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.