awk(linux)

参考:10个超级棒的Awk命令

[user01@server01 ~]$ cat linuxmi.com.txt
1 张三 历史 81 B 0.367
2 李四 物理 72 C 0.588
3 李华 物理 87 B+ 0.677
4 方咪 历史 91 A 0.876
5 陈明 语文 81 B 0.812
6 鱼鱼 英语 81 B 0.571[user01@server01 ~]$
[user01@server01 ~]$ awk '{print $2}' linuxmi.com.txt
张三
李四
李华
方咪
陈明
鱼鱼
[user01@server01 ~]$ awk '{print $0}' linuxmi.com.txt
1 张三 历史 81 B 0.367
2 李四 物理 72 C 0.588
3 李华 物理 87 B+ 0.677
4 方咪 历史 91 A 0.876
5 陈明 语文 81 B 0.812
6 鱼鱼 英语 81 B 0.571
[user01@server01 ~]$ awk /语/'{print $1 $3}' linuxmi.com.txt
5语文
6英语
[user01@server01 ~]$ awk /语/'{print $2 $4}' linuxmi.com.txt
陈明81
鱼鱼81
[user01@server01 ~]$ awk /语/'{print $2,$4}' linuxmi.com.txt
陈明 81
鱼鱼 81
[user01@server01 ~]$ awk /语/'{print $0}' linuxmi.com.txt
5 陈明 语文 81 B 0.812
6 鱼鱼 英语 81 B 0.571
[user01@server01 ~]$ awk /语/'{print $0}' linuxmi.com.txt |awk /语/'{print $0}'
5 陈明 语文 81 B 0.812
6 鱼鱼 英语 81 B 0.571
[user01@server01 ~]$ awk /语/'{print $0}' linuxmi.com.txt |awk /语/'{ print $2,$4}'
陈明 81
鱼鱼 81
[user01@server01 ~]$ awk '$3 ~ /语/{print $2,$4}' linuxmi.com.txt
陈明 81
鱼鱼 81
[user01@server01 ~]$ awk '$3 !~ /语/{print $2,$4}' linuxmi.com.txt
张三 81
李四 72
李华 87
方咪 91
[user01@server01 ~]$ awk '$4 > 81 {print $2,$4}' linuxmi.com.txt
李华 87
方咪 91
[user01@server01 ~]$ awk '$4 > 81 {print $2,$4,$0}' linuxmi.com.txt
李华 87 3 李华 物理 87 B+ 0.677
方咪 91 4 方咪 历史 91 A 0.876
[user01@server01 ~]$ awk '$3~/语/,$3~/理/ {print $2,$4}' linuxmi.com. txt
陈明 81
鱼鱼 81
[user01@server01 ~]$ awk '$3~/语/,$3~/理/ {print $2,$4,$0}' linuxmi.c om.txt
陈明 81 5 陈明 语文 81 B 0.812
鱼鱼 81 6 鱼鱼 英语 81 B 0.571
[user01@server01 ~]$ awk '$3~/语/ || $3~/理/ {print $2,$4,$0}' linuxm i.com.txt
李四 72 2 李四 物理 72 C 0.588
李华 87 3 李华 物理 87 B+ 0.677
陈明 81 5 陈明 语文 81 B 0.812
鱼鱼 81 6 鱼鱼 英语 81 B 0.571

info awk

EXAMPLES
       The awk program specified in the command line is most easily  specified
       within  single-quotes  (for  example, 'program') for applications using
       sh, because awk programs commonly contain characters that  are  special
       to  the shell, including double-quotes.  In the cases where an awk pro‐
       gram contains single-quote characters, it is usually easiest to specify
       most of the program as strings within single-quotes concatenated by the
       shell with quoted single-quote characters. For example:


              awk '/'\''/ { print "quote:", $0 }'

       prints all lines from the  standard  input  containing  a  single-quote
       character, prefixed with quote:.

       The following are examples of simple awk programs:

        1. Write  to  the standard output all input lines for which field 3 is
           greater than 5:


           $3 > 5

        2. Write every tenth line:


           (NR % 10) == 0

        3. Write any line with a substring matching the regular expression:


           /(G|D)(2[0-9][[:alpha:]]*)/

        4. Print any line with a substring containing a 'G' or  'D',  followed
           by  a sequence of digits and characters.  This example uses charac‐
           ter classes digit and alpha to match language-independent digit and
           alphabetic characters respectively:


           /(G|D)([[:digit:][:alpha:]]*)/

        5. Write  any  line  in  which  the  second  field matches the regular
           expression and the fourth field does not:


           $2 ~ /xyz/ && $4 !~ /xyz/

        6. Write any line in which the second field contains a backslash:


           $2 ~ /\\/

        7. Write any line in which the second field contains a backslash. Note
           that  backslash escapes are interpreted twice; once in lexical pro‐
           cessing of the string and once in processing  the  regular  expres‐
           sion:


           $2 ~ "\\\\"

        8. Write the second to the last and the last field in each line. Sepa‐
           rate the fields by a colon:


           {OFS=":";print $(NF-1), $NF}

        9. Write the line number and number of fields in each line. The  three
           strings  representing the line number, the colon, and the number of
           fields are concatenated and that string is written to standard out‐
           put:


           {print NR ":" NF}

       10. Write lines longer than 72 characters:


           length($0) > 72

       11. Write the first two fields in opposite order separated by OFS:


           { print $2, $1 }

       12. Same,  with  input  fields  separated  by  a  comma or <space>s and
           <tab>s, or both:


           BEGIN { FS = ",[ \t]*|[ \t]+" }
                 { print $2, $1 }

       13. Add up the first column, print sum, and average:


                {s += $1 }
           END   {print "sum is ", s, " average is", s/NR}

       14. Write fields in reverse order, one per line  (many  lines  out  for
           each line in):


           { for (i = NF; i > 0; --i) print $i }

       15. Write all lines between occurrences of the strings start and stop:


           /start/, /stop/

       16. Write  all  lines  whose first field is different from the previous
           one:


           $1 != prev { print; prev = $1 }

       17. Simulate echo:


           BEGIN  {
                   for (i = 1; i < ARGC; ++i)
                   printf("%s%s", ARGV[i], i==ARGC-1?"\n":" ")
           }

       18. Write the path prefixes contained in the PATH environment variable,
           one per line:


           BEGIN  {
                   n = split (ENVIRON["PATH"], path, ":")
                   for (i = 1; i <= n; ++i)
                   print path[i]
           }

       19. If there is a file named input containing page headers of the form:


           Page #

       and a file named program that contains:


              /Page/   { $2 = n++; }
                       { print }

       then the command line:


              awk -f program n=5 input

       prints the file input, filling in page numbers starting at 5.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值