ITPub博客

首页 > Linux操作系统 > Linux操作系统 > awk的一些例子

awk的一些例子

原创 Linux操作系统 作者:hjgluguo 时间:2009-08-27 23:41:12 0 删除 编辑
[root@oracle9i shell]# cat fruit_prices.txt
Fruit           Price/lbs       Quantity
Banana          $0.89           100
Peach           $0.79           65
Kiwi            $1.50           22
Pineapple       $1.29           35
Apple           $0.99           78
Mango           $2.20           50
[root@oracle9i shell]# awk '{print;}' fruit_prices.txt
Fruit           Price/lbs       Quantity
Banana          $0.89           100
Peach           $0.79           65
Kiwi            $1.50           22
Pineapple       $1.29           35
Apple           $0.99           78
Mango           $2.20           50
[root@oracle9i shell]# awk '{print$1 $3;}' fruit_prices.txt
FruitQuantity
Banana100
Peach65
Kiwi22
Pineapple35
Apple78
Mango50



[root@oracle9i shell]# awk '{print$1, $3;}' fruit_prices.txt
Fruit Quantity
Banana 100
Peach 65
Kiwi 22
Pineapple 35
Apple 78
Mango 50



[root@oracle9i shell]# awk '{printf "% -15s %s\n", $1, $3;}' fruit_prices.txt
Fruit           Quantity
Banana          100
Peach           65
Kiwi            22
Pineapple       35
Apple           78
Mango           50


[root@oracle9i shell]# awk ' / *\$[1-9][0-9]*\.[0-9][0-9] */ { print $1,$2,$3,"*";} / *\$0\.[0-9][0-9] */ { print ; } ' fruit_prices.txt  Banana          $0.89           100
Peach           $0.79           65
Kiwi $1.50 22 *
Pineapple $1.29 35 *
Apple           $0.99           78
Mango $2.20 50 *


[root@oracle9i shell]# awk ' / *\$[1-9][0-9]*\.[0-9][0-9] */ { print $0,"*";} / *\$0\.[0-9][0-9] */ { print ; } ' fruit_prices.txt
Banana          $0.89           100
Peach           $0.79           65
Kiwi            $1.50           22 *
Pineapple       $1.29           35 *
Apple           $0.99           78
Mango           $2.20           50 *


[root@oracle9i shell]# awk ' $3 <= 75 { printf "%s\t%s\n",$0,"REORDER" ; } $3 > 75 { print $0 ; }' fruit_prices.txt
Fruit           Price/lbs       Quantity
Banana          $0.89           100
Peach           $0.79           65      REORDER
Kiwi            $1.50           22      REORDER
Pineapple       $1.29           35      REORDER
Apple           $0.99           78
Mango           $2.20           50      REORDER


[root@oracle9i shell]# awk ' ( / *\$[1-9][0-9]*\.[0-9][0-9]*/) && ($3 < 45) { printf "%s\t%s\t%s\n",$0,"*","REORDER"; }' fruit_prices.txt
Kiwi            $1.50           22      *       REORDER
Pineapple       $1.29           35      *       REORDER


[root@oracle9i shell]# awk ' ($2 ~ /^\$[1-9][0-9]*\.[0-9][0-9]$/) || ($3 < 75) { printf "%s\t%s\t%s\n",$0,"*","REORDER"; }' fruit_prices.txt
Peach           $0.79           65      *       REORDER
Kiwi            $1.50           22      *       REORDER
Pineapple       $1.29           35      *       REORDER
Mango           $2.20           50      *       REORDER

[root@oracle9i shell]# awk ' ($2 ~ /^\$[1-9][0-9]*\.[0-9][0-9]$/) && ($3 < 75) { printf "%s\t%s\t%s\n",$0,"*","REORDER"; }' fruit_prices.txt
Kiwi            $1.50           22      *       REORDER
Pineapple       $1.29           35      *       REORDER
Mango           $2.20           50      *       REORDER


[root@oracle9i ~]# ls -l
total 27224
-rw-rw-rw-   1 root root       10 Aug 26 08:28 abc.txt
-rw-r--r--   1 root root     1241 Jun 14 02:51 anaconda-ks.cfg
drwxr-xr-x   4 root root     4096 Aug 21 19:25 Desktop
drwxr-xr-x  13 root root     4096 Aug 12 23:00 firefox
-rw-r--r--   1 root root 27699200 Aug  4 18:30 firefox-3.5.2.tar
-rw-r--r--   1 root root    55724 Jun 14 02:51 install.log
-rw-r--r--   1 root root    32909 Jun 14 02:51 install.log.syslog
-rw-r--r--   1 root root        0 Aug 12 20:20 more
drwxr-xr-x   2 root root     4096 Aug 25 11:08 perl
-rw-r--r--   1 root root       10 Aug 26 08:29 sed
drwxr-xr-x   3 root root     4096 Aug 26 08:38 shell
-rwxr-xr-x   1 root root      129 Aug 12 20:10 var_test
[root@oracle9i ~]# ls -l | awk '$1 !~ /total/ { printf "%-32s %s\n",$9,$5; }'
abc.txt                          10
anaconda-ks.cfg                  1241
Desktop                          4096
firefox                          4096
firefox-3.5.2.tar                27699200
install.log                      55724
install.log.syslog               32909
more                             0
perl                             4096
sed                              10
shell                            4096
var_test                         129


[root@oracle9i ~]# cat /etc/passwd | head -5
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@oracle9i ~]# awk -F: '{ print $1, $6 ; }' /etc/passwd | head -5
root /root
bin /bin
daemon /sbin
adm /var/adm
lp /var/spool/lpd

[root@oracle9i shell]# cat fruit_prices.txt
Fruit           Price/lbs       Quantity
Banana          $0.89           100
Peach           $0.79           65
Kiwi            $1.50           22
Pineapple       $1.29           35
Apple           $0.99           78
Mango           $2.20           50

[root@oracle9i shell]# vi awk_var.sh
#!/bin/sh
#awk_var's code
NUMFRUIT="$1"
if [ -z "$NUMFRUIT" ] ; then NUMFRUIT=75 ; fi

awk ' $3 <= numfruit { print ; } ' numfruit="$NUMFRUIT" fruit_prices.txt

[root@oracle9i shell]# ./awk_var.sh
Peach           $0.79           65
Kiwi            $1.50           22
Pineapple       $1.29           35
Mango           $2.20           50

[root@oracle9i shell]# ./awk_var.sh 25
Kiwi            $1.50           22





[root@oracle9i shell]# vi awk_if.sh
#!/bin/sh
# awk_if's code

awk '{
       printf "%s\t",$0;
 if ( $2 ~ /\$[1-9][0-9]*\.[0-9][0-9]/ ) {

        printf " * ";
        if ( $3 <=75 ) {
            printf "REORDER\n" ;
          } else {
                 printf "\n" ;
           }
      } else {
          if ( $3 < 75 ) {
               printf "   REORDER\n" ;
            } else {
              printf "\n";
              }
         }
       }' fruit_prices.txt;

[root@oracle9i shell]# chmod +x awk_if.sh
[root@oracle9i shell]# ./awk_if.sh
Fruit           Price/lbs       Quantity
Banana          $0.89           100
Peach           $0.79           65         REORDER
Kiwi            $1.50           22       * REORDER
Pineapple       $1.29           35       * REORDER
Apple           $0.99           78
Mango           $2.20           50       * REORDER


[root@oracle9i shell]# awk 'BEGIN {x=0; while(x<5) {x+=1; print x;}}'
1
2
3
4
5

[root@oracle9i shell]# awk 'BEGIN { x=0; do { x+=1; print x; } while (x<5) }'
1
2
3
4
5


[root@oracle9i shell]# cat fruit_prices.txt
Fruit           Price/lbs       Quantity
Banana          $0.89           100
Peach           $0.79           65
Kiwi            $1.50           22
Pineapple       $1.29           35
Apple           $0.99           78
Mango           $2.20           50
[root@oracle9i shell]# vi awk_for.sh
#!/bin/sh
# awk_for's code
awk '{
    for (x=1;x<=NF;x+=1) {
         printf "%s  ",$x ;
       }
      printf "\n" ;
     }' fruit_prices.txt

[root@oracle9i shell]# chmod +x awk_for.sh
[root@oracle9i shell]# ./awk_for.sh
Fruit  Price/lbs  Quantity
Banana  $0.89  100
Peach  $0.79  65
Kiwi  $1.50  22
Pineapple  $1.29  35
Apple  $0.99  78
Mango  $2.20  50

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7865774/viewspace-613450/,如需转载,请注明出处,否则将追究法律责任。

上一篇: sed的一些例子
请登录后发表评论 登录
全部评论

注册时间:2008-07-11

  • 博文量
    97
  • 访问量
    182062