Some tips for using the Linux awk command-basic commands

Source: Internet
Author: User

The purpose of the tool is to read the text file line-by-row, then slice the read-in text (by default, separated by a space), and then read it to the next line, which is already available. is a format report generation tool that is then used to process text.

Currently, the awk used on linux is gawk(open source awk), abbreviated to Awk.

Usage:

#awk [Options] ' script ' file,file2,... #awk [options] ' PATTERN {action} ' filelist


quoted words, each line from $ Start, $ As the current line, ending with a newline character as the line

commonly used for: awk ' {print$1} ' File


Common options:

- F : Specify field separators awk-f: ' {print '} '/etc/passwd

- V: assigning an initial value to a variable


print function, printing content, with line break

The printf function, which formats the printed content without line breaks, requires the following format:

awk ' {printF ' format ' $} ' file

The format is as follows:

in % begins with a character followed by a

%c Display character ASCII code%d,%i: decimal number%e,%e scientific notation display numerical%g,%g scientific notation format or floating-point format display numeric value%f display floating-point number%u unsigned integer percent display% self%s display string


Modifier

N, such as%10s, the current string occupies 10 cells, not enough to fill empty-: left-aligned, default right-aligned +: Display numeric symbols


Example

Linux1:/home/test #awk ' {printf '%-15s%i\n ", $1,$2} ' 1welcome 0hello 0

Output redirection

Printitems > Output-file

Printitems >> Output-file

Printitems | Command

Special file descriptors

/dev/stdin: Standard input

/dev/stdout Standard Output

/dev/stderr : Error Output

/dev/fd/n a specific file descriptor, such as /dev/stdin is equivalent to /dev/fd/0

For example:

Linux1:/home # awk-f: ' {printf '%-15s%i\n ',$1,$3> '/dev/stderr '} '/etc/passwdroot 0bin 1daemon 2LP 4mail 8games 12wwwrun 30ftp 40nobody 65534



This article is from the "testing of the Rhythm" blog, please be sure to keep this source http://fociceo.blog.51cto.com/2480/1664449

Some tips for using the Linux awk command-basic commands

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.