Examples of character truncation commands (cut, printf, sed, and awk), sedawk

Source: Internet
Author: User

Examples of character truncation commands (cut, printf, sed, and awk), sedawk
1. cut

-F column number: column number extracted-d separator: column separated by the specified separator
2. printf
% Ns: Output string. Output nth string % ni: Output integer. % M. nf: Output floating point number. Indicates the number of m digits in the output, where n digits are decimals and (m-n) digits are integers \ a: Output warning sound \ B: output return key, that is, the Backspace key \ f: clear screen \ n: newline \ r: carriage return, that is, Enter key \ t: horizontal output return key \ v: vertical output return key
3. sed


// Start from Baidu Baike ^ anchor, for example,/^ sed/matches all rows starting with sed. $ The End Of The Anchor row is as follows:/sed $/matches all rows ending with sed .. Match a non-linefeed character, for example,/s. d/match s, followed by any character, and then d. * Match zero or multiple characters, such as:/* sed/match all templates with one or more spaces followed by sed rows. [] Matches a character in a specified range, such as/[Ss] ed/matches sed and Sed. [^] Match a character that is not within the specified range, for example:/[^ A-RT-Z] ed/match a line that is followed by a letter that does not contain a A-R and a T-Z. \ (.. \) Stores matched characters, such as s/\ (love \) able/\ 1rs, and loveable is replaced with lovers. &; Save the search characters to replace other characters, such as s/love/** & **/. The love character is ** love **. \ <; Specifies the start of a word, for example :/\
; Anchor specifies the end of a word, for example,/love \>/matches a row that contains a word ending with love. X \ {m \} repeated characters x, m times, such as:/0 \ {5 \}/matched rows containing 5 o. The characters x \ {m, \} are repeated at least m times, for example,/o \ {5, \}/matches rows with at least 5 o. The characters x \ {m, n \} must be repeated at least m times, not more than n times, such as:/o \ {5, 10 \}/matching rows of 5--10 o.


// Delete from Baidu Encyclopedia: Command d $ sed '2d 'example ----- Delete the second line of the example file. $ Sed '2, $ d' example ----- delete all rows from the second row to the end of the example file. $ Sed '$ d' example ----- Delete the last row of the example file. $ Sed '/test/'d example ----- delete all rows containing test in the example file. Replace: s command $ sed's/test/mytest/G' example ----- replace test with mytest in the entire line. If no g tag exists, only the first matched test in each row is replaced with mytest. $ Sed-n's/^ test/mytest/p 'example ----- (-n) option and the p Flag are used together to print only the replaced rows. That is to say, if the test at the beginning of a row is replaced with mytest, print it. $ Sed's/^ localhost/'example ----- &; symbol represents the part found in the replacement string. All rows starting with are replaced with their own localhost and changed to $ Sed-n's/\ (love \) able/\ 1rs/p 'example ----- love is marked as 1, and all loveable will be replaced with lovers, the replaced line is printed out. $ Sed's #10 #100 # g'example ----- whatever the character, followed by the s command is considered as a new separator. Therefore, "#" is a separator here, replaces the default "/" separator. Replace all 10 with 100. The range of selected rows: comma $ sed-n'/test/,/check/P' example ----- all rows within the range specified by the template test and check are printed. $ Sed-n' 5,/^ test/P' example ----- print all rows starting from the fifth line to the first line that contains the beginning of test. $ Sed '/test/,/check/s/$/sed test/' example ----- for the rows between the template test and check, the end of each row is replaced by the string sed test. Multi-Point Editing: The e command $ sed-e '1, 5d '-e's/test/check/'example ----- (-e) option allows multiple commands to be executed in the same line. As shown in the example, the First Command deletes lines 1 to 5, and the second command replaces test with check. The command execution sequence has an impact on the result. If both commands are replacement commands, the first replacement command will affect the result of the second replacement command. $ Sed -- expression ='s/test/check/'-- expression ='/love/d' example ----- a better command than-e is -- expression. It can assign values to sed expressions. Read from a file: the content in the r command $ sed '/test/r file 'example ----- file is read and displayed after the line Matching test. If multiple lines match, the file content is displayed under all matched rows. Write file: w command $ sed-n'/test/w file' example ----- in example, all rows containing test are written into file. APPEND command: a command $ sed '/^ test/a \ ---> this is a example 'example <----- 'this is a example' is appended to the end of the row starting with test, sed requires that command a be followed by a backslash. Insert: I command $ sed '/test/I \ new line ------------------------- 'example if test is matched, insert the text following the backslash to the front of the matching line. Next: n command $ sed '/test/{n; s/aa/bb/;}' example ----- if test is matched, move it to the next line of the matching line, replace the aa of this row with bb, print the row, and continue. Deformation: The y command $ sed '1, 10y/abcde/ABCDE/'example ----- converts all abcde in line 1-10 to uppercase. Note that this command cannot be used for the Regular Expression metacharacters. Exit: q command $ sed '10q' example ----- exit sed after printing the 10th lines. Keep and get: h command and G command $ sed-e '/test/H'-e'/$/G' example ----- when sed processes files, each row is saved in a temporary buffer called the mode space. Unless the row is deleted or the output is canceled, all processed rows are printed on the screen. The mode space is cleared, and a new row is saved for processing. In this example, the row Matching test is found and saved to the mode space. The h Command copies the row and saves it to a special buffer zone called keep cache. The second statement means that when the last line is reached, the G command extracts the row that maintains the buffer and places it back in the mode space, and append to the end of the row that already exists in the mode space. In this example, It is appended to the last row. Simply put, any row containing test is copied and appended to the end of the file. Keep and swap: The h command and the x command $ sed-e '/test/H'-e'/check/X' example ----- swap mode space and keep the buffer content. That is, to swap the rows containing test and check.
4. awk supports print and printf commands in awk output.

Print: print will automatically add a line break after each output (by default, there is no print command in Linux)
Printf: printf is a standard output command and does not automatically add line breaks. If you need to wrap the lines, manually add line breaks.

Awk format
# Awk 'condition 1 {Action 1} condition 2 {Action 2 }... 'example: [root @ foundation8 yum. repos. d] # cat student.txt Name AgeHX 21hh 20 [root @ foundation8 yum. repos. d] # awk '{printf $1 "\ n"}' student.txt NameHXhh

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.