[awk]--syntax -02

来源:互联网 发布:手机淘宝版本5.9.8 编辑:程序博客网 时间:2024/05/23 00:03

tip:for this section about fundament syntax of  awk


 1.1 how to run a awk's program
 1.2 fundamental syntax #newline sperate terminate

 1.3 comment  a statement

------------------------------------------

the file "data"  for illustration

[oracle@localhost ~]$ cat data
footslw
shfootch
choing

------------------------------------------

answer:1.1

(1)

#with the infile parameter

[oracle@localhost ~]$ awk '{print substr($1,1,1)}' data
f
s
c

(2)

#with no infile parameter

[oracle@localhost ~]$ awk 'BEGIN { print "have no infile parameter!"}'    
have no infile parameter!

(3)

[oracle@localhost ~]$ ls -tl |head
total 83280
-rw-r--r-- 1 oracle oinstall      771 Aug  2 01:32 data_bak2
-rwxr-xr-x 1 oracle oinstall      771 Jul 31 23:14 data_bak
-rwxr-xr-x 1 oracle oinstall       81 Jul 31 20:16 sh02
-rwxr-xr-x 1 oracle oinstall       53 Jul 31 19:53 shell01
-rwxr-xr-x 1 oracle oinstall      771 Jul 31 19:10 d2 a
-rw-r--r-- 1 oracle oinstall       25 Jul 31 18:28 data
-rw-r--r-- 1 oracle oinstall      101 Jul 31 01:36 tmpdata3
-rw-r--r-- 1 oracle oinstall      280 Jul 31 00:28 001b
-rw-r--r-- 1 oracle oinstall      232 Jul 30 23:46 002
[oracle@localhost ~]$ ls -tl |head |awk '{print $1,$4}'
total
-rw-r--r-- oinstall
-rwxr-xr-x oinstall
-rwxr-xr-x oinstall
-rwxr-xr-x oinstall
-rwxr-xr-x oinstall
-rw-r--r-- oinstall
-rw-r--r-- oinstall
-rw-r--r-- oinstall
-rw-r--r-- oinstall


[oracle@localhost ~]$ ls -tl |head |awk '{print $1,$4}' |head -3
total
-rw-r--r-- oinstall
-rwxr-xr-x oinstall

(4):awk -f awkfile

[oracle@localhost ~]$ awk -f awkf data2
-rw-r--r-- oinstall
ok
-rw-r--r-- oinstall
ok
-rwxr-xr-x oinstall
ok
[oracle@localhost ~]$ cat awkf
#!/bin/awk
{print $1,$4};
{print "ok"};


answer 1.2

(1) newline with continue(n)

[oracle@localhost ~]$ ls -tl |head |awk '{print $5}'|he\
> ad -3

771
771

it is equivalent  to command "ls -tl |head |awk '{print $5}'|head -3


(2) line with terminate

[oracle@localhost ~]$ awk '{print $1;}
{print $2;}
' data2
-rw-r--r--
1
-rw-r--r--
1
-rwxr-xr-x
1
[oracle@localhost ~]$ awk '{print $1}
{print $2}
' data2
-rw-r--r--
1
-rw-r--r--
1
-rwxr-xr-x
1
[oracle@localhost ~]$ awk '{print $1;print $2}' data2
-rw-r--r--
1
-rw-r--r--
1
-rwxr-xr-x
1
[oracle@localhost ~]$ awk '{print $1                      
print $2}' data2
-rw-r--r--
1
-rw-r--r--
1
-rwxr-xr-x
1


[oracle@localhost ~]$ awk '{print $1};{print $2}' data2
-rw-r--r--
1
-rw-r--r--
1
-rwxr-xr-x
1
[oracle@localhost ~]$ awk '{print $1}{print $2}' data2
-rw-r--r--
1
-rw-r--r--
1
-rwxr-xr-x
1

#we can see that
 "{ } ,\r(newline)"can instead of semicolon to terminate a command;


andwer 1.3


[oracle@localhost ~]$ awk '{print $1}
> #this is a comment;
> {print $2};
> ' data2
-rw-r--r--
1
-rw-r--r--
1
-rwxr-xr-x
1

#there is no multiline in awk;