linux / unix 셸 의 awk

오늘 awk 사용법 을 배 워 서 여러분 과 공유 하고 있 습 니 다.
준비 작업:
먼저 준비 한 ls. out 의 내용 은 다음 과 같다.
[oracle@localhost testDir]$ cat ls.out 
total 68
-rwxr--r-- 1 oracle oinstall  885 Feb 23 16:23 awk.sh
-rw-r--r-- 1 oracle oinstall  216 Feb 23 11:53 blank_file
-rwxr--r-- 1 oracle oinstall  642 Feb 15 18:43 find.sh
drwxr-xr-x 2 oracle oinstall 4096 Feb 15 17:56 folder
-rw-r--r-- 1 oracle oinstall    0 Feb 23 16:32 ls.out
-rwxr-xr-x 1 oracle oinstall   26 Nov 10 22:30 main.sh
-rwxrwxrwx 1 oracle oinstall   44 Jan  7 22:39 shelltest.sh
-rwxr-xr-x 1 oracle oinstall   99 Feb 23 11:51 test1.sh
-rwxr-xr-x 1 oracle oinstall   99 Feb 23 11:52 test2.sh

사실은 ls - l > ls. out 이 특정한 디 렉 터 리 에 저 장 된 파일 목록 입 니 다.
바로 본론 으로 들어가다
#           :
ls -l | awk ' /^[^d]/ {print $9"\t"$5; total+=$5} END {print "the total size of the files is:"total}'
awk.out 121
awk.sh  2250
blank_file      216
find.sh 642
ls.out  511
main.sh 26
shelltest.sh    44
test1.sh        99
test2.sh        99
the total size of the files is:4008

#    279  ,   289   
awk 'gsub(/279/,289) {print $0}' ls.out

#  xr       ,  0     
awk '{print index($1, "xr")"   "$1}' ls.out
0   total
4   -rwxr--r--
0   -rw-r--r--
4   -rwxr--r--
4   drwxr-xr-x
0   -rw-r--r--
4   -rwxr-xr-x
4   -rwxrwxrwx
4   -rwxr-xr-x
4   -rwxr-xr-x

#       awk.sh            
awk '$9=="awk.sh" {print length($9)"   "$9}' ls.out
6   awk.sh

#         test        ,  0     
awk '{print match($9, "test")"   "$9}' ls.out
0  
0   awk.sh
0   blank_file
0   find.sh
0   folder
0   ls.out
0   main.sh
6   shelltest.sh
1   test1.sh
1   test2.sh

# #    ,     123#456#789     myarray ,        
awk 'BEGIN {print split("123#456#789", myarray, "#")}'
3

#          test1.sh      99 100,  
awk '$9=="test1.sh"{print sub(/99/, "100", $0)} {print $0}' ls.out
total 68
-rwxr--r-- 1 oracle oinstall  885 Feb 23 16:23 awk.sh
-rw-r--r-- 1 oracle oinstall  216 Feb 23 11:53 blank_file
-rwxr--r-- 1 oracle oinstall  642 Feb 15 18:43 find.sh
drwxr-xr-x 2 oracle oinstall 4096 Feb 15 17:56 folder
-rw-r--r-- 1 oracle oinstall    0 Feb 23 16:32 ls.out
-rwxr-xr-x 1 oracle oinstall   26 Nov 10 22:30 main.sh
-rwxrwxrwx 1 oracle oinstall   44 Jan  7 22:39 shelltest.sh
1
-rwxr-xr-x 1 oracle oinstall   100 Feb 23 11:51 test1.sh
-rwxr-xr-x 1 oracle oinstall   99 Feb 23 11:52 test2.sh

#    
awk '$9=="awk.sh" {print substr($9, 1, 2)}' ls.out
aw

#      
awk 'BEGIN {STR="lubinsu is the best"} END{print substr(STR, 3)}' ls.out
binsu is the best

#        ,      
STR="lubinsu is the best!"
echo $STR | awk 'END {print substr($STR, 1)}' >> awk.out
lubinsu is the best!

#        :
awk '{printf "%-15s %s
", $1, $9}' ls.out total -rwxr--r-- awk.sh -rw-r--r-- blank_file -rwxr--r-- find.sh drwxr-xr-x folder -rw-r--r-- ls.out -rwxr-xr-x main.sh -rwxrwxrwx shelltest.sh -rwxr-xr-x test1.sh -rwxr-xr-x test2.sh # awk '{if ($5 < size) print $0}' size=1000 ls.out total 68 -rwxr--r-- 1 oracle oinstall 885 Feb 23 16:23 awk.sh -rw-r--r-- 1 oracle oinstall 216 Feb 23 11:53 blank_file -rwxr--r-- 1 oracle oinstall 642 Feb 15 18:43 find.sh -rw-r--r-- 1 oracle oinstall 0 Feb 23 16:32 ls.out -rwxr-xr-x 1 oracle oinstall 26 Nov 10 22:30 main.sh -rwxrwxrwx 1 oracle oinstall 44 Jan 7 22:39 shelltest.sh -rwxr-xr-x 1 oracle oinstall 99 Feb 23 11:51 test1.sh -rwxr-xr-x 1 oracle oinstall 99 Feb 23 11:52 test2.sh # : df -k | awk '($4 ~/^[0-9]/) {printf("%-15s %s
", $6, $4)}' / 8254824 /boot 265512 /dev/shm 517676 # who | awk '{print $1 " is logged on"}' root is logged on oracle is logged on root is logged on # awk 'BEGIN{record="1234#567#890";split(record, myarray, "#")} END{for (i in myarray){print myarray[i]}}' /dev/null 1234 567 890

좋은 웹페이지 즐겨찾기