今天學習了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\n", $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\n", $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