[Shell] character intercept command: Cut, printf, awk, sed

Source: Internet
Author: User
Tags clear screen disk usage

-------------------------------------------------------------------------------------------

"Cut Command"

Cut [Options] File

-F Column Number (--field extract the first few columns)

-D delimiter (--delimiter splits columns by specified delimiter)

VI User.txt

(The default delimiter for the row separator is tab tab,cut is a tab character)

    

  

  

Extract the user name and UID of the system:

Cut-d ":"-F 1,3/ETC/PASSWD

Used in combination with grep: (assuming that you add 100 ordinary users in bulk, or if you need to delete them in bulk, you need to extract the name of a normal user)

Useradd user1

Useradd User2

Useradd User3

(All system User Login directory is/sbin/nologin, by matching/bin/bash to extract all the users can log in,-V to select the row that does not match the root, and then use the Cut matching column to extract the user name; Assign the result to a variable and delete it by loop)

cat/etc/passwd | grep "/bin/bash" | Grep-v "Root" | Cut-d ":"-F 1

Limitations of the Cut command:

(Extract system disk usage)

Df-h

df-h | grep "Sda3" | Cut-f 5 # can only match the travel all content, because the delimiter is the space, is not the tab

? df-h | grep "Sda3" | Cut-d ""-F 5 # cannot match the desired result because the delimiter is a strict match and if the actual content delimiter has two spaces, the correct result cannot be obtained

"printf"

printf ' output type output format ' content

Output Type:

%ns: Output string. N is a number, which refers to the output of several characters

%ni: Output integer. N is a number, which means outputting several numbers

%M.NF: Output floating-point number. M and n are numbers that refer to the integer and decimal digits of the output, respectively. such as%8.2f represents a total output of 8 digits, where 2 is a decimal, and 6 is an integer.

Output format:

\a: Output Warning sound

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

\f: Clear Screen

\ n     : line Break

\ r     : carriage return, which is the ENTER key

\ t     : the horizontal output backspace key, which is the TAB key

\v: Vertical output backspace key, i.e. TAB key

printf%s 1 2 3 4 5 6 # Take 123456 as a string output, no formatting

Printf%s%s%s 1 2 3 4 5 6 #%s%s123456 as a string output, no format

printf '%s ' 1 2 3 4 5 6 # put 1 2 3 4 5 6 as a string output with a blank output format

printf '%s\n ' 1 2 3 4 5 6 # Output format is 1 rows

printf '%s%s '%s ' 1 2 3 4 5 6 # Content As String three for a set of outputs, 1 2 34 5 6

printf '%s%s%s\n ' 1 2 3 4 5 6 # Output format is 3 rows

printf '%s ' $ (cat user.txt) # Output text content is a string

printf '%s\t%s\t%s\t%s\n ' $ (cat user.txt) # Format output of text content

"Awk"

The standard output command for awk is printf, and the default delimiter is a space or tab. The Cut command cannot intercept columns, tabs, or specific delimiters in a string where the style character is a space.

1. awk ' condition 1{action 1} Condition 2{action 2} ... ' File name

(If condition 1, perform action 1; If condition 2, perform action 2)

Condition (pattern):

General use of relational expressions as criteria

X > 10 determine if the variable x is greater than 10

x>=10 greater than or equal to

x<=10 less than or equal to

Actions (Action)

Formatted output

Process Control Statements

  

   awk ' {printf $ "\ t" $ "\ n"} ' User.txt

(There are no conditions in front of the curly braces, directly execute the command, here printf is the command of AWK, the $ $ extract file in the second column, $ $ Extract the third column, $ fetch all columns)

   df-h | awk ' {print $ \ t ' $ "\ t" $6 "\ T"} '

(Print three columns, here the print is Awk's command, the system does not have the print command, so it can only be used in awk; the difference with printf is that print automatically adds line breaks at the end of the row, and printf does not)

   df-h | grep Sda3 | awk ' {print $} ' | Cut-d '% '-f 1

(The extraction system has used hard disk space, you can assign the result to a variable, to determine whether it is greater than a certain value, alarm)

2. Begin: Perform an extra action first

   awk ' Begin{print ' This is a text '} {print $ "\ T" $ user.txt} '

3. END: Used to execute after all commands have been processed

4. FS Built-in variables: used to define the delimiter, if you need to manually define the delimiter, be sure to precede the delimiter to begin;

   awk ' begin{fs= ': "} end{print" This is END text "} {print $ \ t" $ $} '/etc/passwd

5. Relational operators:

   Cat User.txt | Grep-v ID | awk ' $4 > {printf $ "\ n"} '

(User.txt does not contain the ID row, extraction satisfies the second column with the fourth column value greater than 18)

"SED"

SED is a lightweight flow editor that is almost included on all UNIX platforms. SED is primarily used to select, replace, delete, and add commands to the data. (can be placed after pipe character processing)

sed [options] ' [action] ' file name

  

Options:

-N: The general sed command will output all data to the screen, and if this option is added, only the lines processed by the SED command will be output to the screen.

Sed-n ' 2p ' user.txt # output second line

-e: Allow multiple sed command edits to input data

-I: Directly modifies the file that reads the data with the result of the SED modification, rather than the screen output

Action: (double quotation marks are added)

A\: Append to add one or more rows after the current line. When multiple rows are added, the "\" at the end of each line is required to indicate that the data is not finished except for the last row.

C\: Row substitution, replacing the original data row with a string after C, replacing multiple rows, except for the last row, the end of each line needs "\" to represent the data is not finished.

I\: INSERT, insert one or more rows before the current line. When multiple rows are inserted, the "\" at the end of each line is required to indicate that the data is not finished except for the last row.

D: Delete, delete the specified line.

P: Print, output the specified line.

S: string substitution, replacing another string with one. The format is "line range s/old string/new string/g" (similar to the replacement format in vim)

  sed-n ' 2p ' user.txt # outputs the second line, p is usually used with-N, and no-n will show all rows

  df-h | sed-n ' 2p ' # pipe break result as action content

  sed ' 2,4d ' user.txt # Delete the 2nd line of the file to line 4th, show the remaining lines, no add i option, do not change the file contents

  sed ' 2a hello ' user.txt # append Hello after the second line, just modify the command output

  sed ' 2i hello \

World ' User.txt # inserts two rows of data before the second line, only modifies the command output

  sed ' 2c no person ' user.txt # Replace the second line with no person

  sed ' 2s/m/f/g ' user.txt # replaces the second line of M with the F post output

  

  sed-i ' 2s/m/f/g ' user.txt # write the replaced result to a file

  sed-e ' s/zhang//g; s/wang//g ' User.txt #-E allows multiple commands to be executed in sequence, separated by semicolons, preceded by a number representing all lines

Link:http://www.cnblogs.com/farwish/p/4806018.html

@ Black eyed poet <www.farwish.com>

[Shell] character intercept command: Cut, printf, awk, sed

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.