Awk and awk commands

Source: Internet
Author: User

Awk and awk commands

Awk usage:

Move file: ls-lrt | awk '{if (substr ($20111029, 8) = '000000' & substr ($, 26, 3) = 'unl ') print "mv" $8 "/home/etl/data/interface;"} '| ksh

Sum: awk '{sum + = $1} END {print sum }'

Delete a file: ls-lrt | awk '{if (substr ($20111029, 8) = '000000 ') print "rm" $8 "/home/etl/data/interface;"} '| ksh

Find./-name "* 20111113 *" | awk '{print "rm" $1}' | ksh

Find file: find. -name "* sms20111201 *. unl "| xargs awk-F' |'' {if ($39 = '-1') print FILENAME "|" $0 }'


Awk-F Problems

-F indicates the delimiter of the FS Field. The default Delimiter is space.
If you want to change the output field separator to OFS (the output field separator, the default is space), as shown in
Echo a B c d | awk '{OFS = "|"; print $1, $2, $3, $4 }'
However, the output result of $0 is invalid.

So there are two methods:
1. awk '{OFS = "|"; if ($5 = "0") print $1, $2, $3, $4, $5, $6, $7, $8, $9, "this file is blank"} 'bb | tee cc
2. Because ls-l Results contain not only spaces but also tabs (\ t), you can use the following statements to meet your requirements.
Awk '{if ($5 = "0 ") print $0} 'bb | perl-pe '{s/[\ t] +/\ |/g}' | tee cc

Awk-F Problems

-F indicates the delimiter of the FS Field. The default Delimiter is space.
If you want to change the output field separator to OFS (the output field separator, the default is space), as shown in
Echo a B c d | awk '{OFS = "|"; print $1, $2, $3, $4 }'
However, the output result of $0 is invalid.

So there are two methods:
1. awk '{OFS = "|"; if ($5 = "0") print $1, $2, $3, $4, $5, $6, $7, $8, $9, "this file is blank"} 'bb | tee cc
2. Because ls-l Results contain not only spaces but also tabs (\ t), you can use the following statements to meet your requirements.
Awk '{if ($5 = "0 ") print $0} 'bb | perl-pe '{s/[\ t] +/\ |/g}' | tee cc

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.