今天差不多敲打了一天,对awk有新的认识,感觉都是另一个shell了,知识点很多。
一,理论知识
image.png
image.png
image.png
image.png
二,实践
862 awk -F: '$3<=10' /etc/passwd
863 awk -F: '$3>100' /etc/passwd
864 awk -F: '$3<=10{print $1}' /etc/passwd
865 awk 'NR==4' /etc/passwd
866 awk -F: '$3>1&&$3<5' /etc/passwd
867 awk -F: '$3==1&&$3==5' /etc/passwd
868 awk -F: '$3==1||$3==5' /etc/passwd
869 awk 'BEGIN{print "OK"}'
870 awk -F: 'BEGIN{print "用户名 UID 解释器"} \'
871 awk -F: 'BEGIN{print "用户名 UID 解释器"} \
872 {print $1,$3,$7} \
873 END {print "总计有"NR"个帐户."}' /etc/passwd
874 awk -F: 'BEGIN{print "用户名 UID 解释器"} \
875 {print $1,$3,$7} \
876 END {print "总计有"NR"个帐户."}' /etc/passwd | column -t
877 awk 'BEGIN{print 2**3}'
878 awk 'BEGIN{print 10-4}'
879 awk 'BEGIN{x=8;y=2;print x*y}'
880 awk 'BEGIN{x=8;x*=2;print x}'
881 awk '/bash$/{x++} END{print x}' /etc/passwd
882 who
883 who | awk '$1=="root"{x++} END{print x}]
884 '
885 who | awk '$1=="root"{x++} END{print x}'
886 seq 200 |awk '$1%7==0 && $1~/7/'
887 df
888 df |tail -n +2
889 df |tail -n +2 |awk 'sum+=$4 END {print sum}'
890 df |tail -n +2 |awk '{sum+=$4} END {print sum}'
891 ls -l /etc/*.conf
892 ls -l /etc/*.conf|awk '{sum+=$5} END{print sum}'
893 ls -l /etc
894 ls -l /etc |awk '/^~/{sum+=$5} END{print sum}'
895 ls -l /etc |awk '/^-/{sum+=$5} END{print sum}'
896 ps -eo user,pid,pcpu,comm
897 ps -eo user,pid,pcpu,comm|awk '{ if ($3>0.5) {print} }'
898 cd /etc/sysconfig/network-scripts/
899 vim ifcfg-enp0s3
900 reboot
901 ps -eo user,pid,pcpu,comm
902 ps -eo user,pid,pcpu,comm|awk '{ if($3>0.5) {print} }'
903 ps -eo user,pid,pcpu,comm|awk '{ if($3>0.0) {print} }'
904 ps -eo user,pid,rsss,comm|awk '{ if($3>16) {print} }'
905 ps -eo user,pid,rss,comm|awk '{ if($3>16) {print} }'
906 ps -eo user,pid,rss,comm|awk '{ if($3>1024) {print} }'
907 useradd rick
908 useradd vicky
909 awk -F: '{if($3<1000){x++}else{y++}} \
910 END{print x, y}' /etc/passwd
911 awk -F: '{if($3<1000){x++}else{y++}} \
912 END{print x, y}' /etc/passwd
913 ls -l /etc | awk '{
914 if ($1~/^-/) {x++} else {y++} \
915 }
916 END {print x, y}'
917 seq 10 |awk '{
918 if ($1%2==0) {print $1"is odd";x++} else {print $1"is even";y++}
919 }
920 END {print x,y}
921 '
922 seq 10 |awk '{
923 if ($1%2==0) {print $1"is odd";x++} else {print $1"is even";y++}
924 }
925 END {print x,y}
926 '
927 awk 'BEGIN{a[0]=11;print a[0]}''
928 awk 'BEGIN{a[0]=11;print a[0]}'
929 awk 'BEGIN{a[0]=11;print a["0"]}'
930 awk 'BEGIN{ \
931 tome["age"]=22; \
932 tome["addr"]="beijing";
933 print tome["age"], tome["addr"] \
934 }'
935 awk 'BEGIN{ \
936 a[10]=11;a[88]=22;a["book"]=33;a["work"]="home";\
937 for (i in a) {print i}
938 }'
939 df
940 df |awk 'NR!=1 {disk[$1]=$4} \
941 END {for (i in disk) {printf i, disk[i]}
942 }'
943 df |awk 'NR!=1 {disk[$1]=$4} \
944 END {for (i in disk) {printf disk[i]}
945 }'
946 df |awk 'NR!=1 {disk[$1]=$4} \
947 END {for (i in disk) {printf "-20s %-10s\n", i, disk[i]}
948 }'
949 df |awk 'NR!=1 {disk[$1]=$4} \
950 END {for (i in disk) {printf "%-20s %-10s\n", i, disk[i]}
951 }'
952 awk 'BEGIN{ for (i=1;i<=5;i++) {print i}}'
953 awk '{for (i=1;i<=NF;i++) {if ($i=="is") x++ }} END {print x}' test.txt
954 ll
955 awk '{for (i=1;i<=NF;i++) {if ($i=="is") x++ }} END {print x}' ver2.txt
956 awk 'BEGIN{ i=1;while(i<=5) {print i;i++} }'
957 cd shell_demo/
958 vim test.txt
959 awk '{i=1;while(i<=NR) {printf "%-2d", $i; i++} ; print " "}' test.txt
960 awk 'BEGIN {
961 i=0;
962 while(i<=5) {
963 i++;
964 if (i==3) {continue};
965 print i
966 };
967 print "over"
968 END {print "end"} ' /etc/hosts
969 awk 'BEGIN {
970 i=0;
971 while(i<=5) {
972 i++;
973 if (i==3) {continue};
974 print i
975 };
976 print "over" }
977 END {print "end"} ' /etc/hosts
978 awk 'BEGIN {
979 i=0;
980 while(i<=5) {
981 i++;
982 if (i==3) {break};
983 print i
984 };
985 print "over" }
986 END {print "end"} ' /etc/hosts
987 awk 'BEGIN {
988 i=0;
989 while(i<=5) {
990 i++;
991 if (i==3) {exit};
992 print i
993 };
994 print "over" }
995 END {print "end"} ' /etc/hosts
996 df -h
997 df -h|awk '{print $4}'
998 df -h |awk '{if(NF==1){getline; print$3};if(NF==6) {print$4}}'
999 cd shell_demo/
1000 history
1001 vim test.txt
1002 awk '/air/{getline;print "netx line:",$0} print "normal line"}' test.txt
1003 awk '/air/{getline;print; "netx line:",$0} print "normal line"}' test.txt
1004 awk '/air/{getline;print; "netx line:",$0} {print "normal line"}' test.txt
1005 awk '/air/{getline;print "netx line:",$0} {print "normal line"}' test.txt
1006 awk '/air/{next;print "netx line:",$0} {print "normal line"}' test.txt
1007 awk 'BEGIN{system("ls")}'
1008 awk 'BEGIN{system("echo test")}'
1009 awk 'BEGIN{system("uptime")}'
1010 awk '{system("echo date:"$0)}' test.txt
1011 awk 'BEGIN{print cos(50)}'
1012 awk 'BEGIN{print sin(45)}'
1013 awk 'BEGIN{print sqrt(45)}'
1014 awk 'BEGIN{print int(4.5)}'
1015 awk 'BEGIN{print rand()}'
1016 awk 'BEGIN{srand(); print rand()}'
1017 awk 'BEGIN{test="hello the world"; print length(test)}'
1018 cat /etc/shells
1019 awk '{print length()}' /etc/shells
1020 awk 'BEGIN{test="hello the world";print index(test,"h")};
1021 awk 'BEGIN{test="hello the world";print index(test,"h")}'
1022 awk 'BEGIN{test="hello the world";print index(test,"t")}'
1023 awk 'BEGIN{print match("How much? 981$", "[0-9]")}'
1024 awk 'BEGIN{print match("How much? 981$", "[a-z]")}'
1025 awk 'BEGIN{print tolower("THIS IS A TEst")}'
1026 awk 'BEGIN{apple="red aPPle";print toupper(apple[0])}'
1027 awk 'BEGIN{apple="red aPPle";print toupper(apple)}'
1028 awk 'BEGIN{split("hello the world", test);print test[3]}'
1029 awk 'BEGIN{split("hello:the:world",test);print test[3]}'
1030 awk 'BEGIN{split("hello:the:world",test);print test[1]}'
1031 awk 'BEGIN{split("hello:the:world",test,":");print test[1]}'
1032 awk 'BEGIN{hi="hello world";gsub("o","O",hi; print hi}'
1033 awk 'BEGIN{hi="hello world";gsub("o","O",hi); print hi}'
1034 head -l /etc/passwd
1035 head -1 /etc/passwd
1036 head -1 /etc/passwd |awk '{gsub("[0-9]","**");print $0}'
1037 head -1 /etc/passwd |awk '{sub("[0-9]","**");print $0}'
1038 awk 'BEGIN{hi="Hello World"; print substr(hi, 2, 3)}'
1039 awk 'BEGIN{print systime()}'
1040 awk '
function max(x,y) {
if (x>y) {print x}
else {print y}}
BEGIN{max(18,9)}'
网友评论