今天学习了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