awk

mv xiong.txt xiong1.txt 修改名称

awk '{print}' xiong.txt 把xiong.txt里每行内容都输出屏幕上

awk -f xiong1.awk xiong.txt 使用独立脚本文件向awk传输命令参数

awk -F ";" '{print "the 3th: " $3,";","the 4th: "$4}' xiong.txt 指定;为字段分隔符,将每行第3个和第4个字段取出输出屏幕

以脚本形式设定分隔符
xiong.awk:
BEGIN{
FS=";"
}

{print "the 3th: " $3,";","the 4th: " $4}

awk -f xiong.awk xiong.txt

 

 

awk '/xiong/{print}' xiong.txt 查找带有字符xiong的行,并将改行内容展示出来

将第二个字段内容等于xiong的所有行的第3个,第4个字段内容输出展示
xiong.awk
BEGIN{
FS=","
}

$2=="xiong" {print $3,$4}

awk -f xiong.awk xiong.txt

将第二个字段能匹配xiong的所有行的第3个,第4个字段内容输出展示
xiong.awk
BEGIN{
FS=","
}

$2~/xiong/ {print $3,$4}

awk -f xiong.awk xiong.txt

将第二个字段不能匹配xiong的所有行的第3个,第4个字段内容输出展示
xiong.awk
BEGIN{
FS=","
}

$2!~/xiong/ {print $3,$4}

 

或运算
xiong.awk
BEGIN{
FS=","
}

($2=="xiong")||($3=="hello") {print $3,$4}

awk -f xiong.awk xiong.txt

与运算
xiong.awk
BEGIN{
FS=","
}

($2=="xiong")&&($2=="hello") {print $3,$4}

awk -f xiong.awk xiong.txt

 

字符串变量,查询文件里有多少行空行
xiong.awk
BEGIN {x="0"}
/^$/ {x=x+1}
END {print "I found "x" blank lines"}


处理一个记录占用三行的文本
address
xiongxiong
xiong@hantian.cn
12345679873

gouzi
gouzi@xueersi.cn
873212349873

xiong.awk
BEGIN{
FS="\n"
RS=""
}
{
print $1 "," $2 "," $3
}

xiong.awk
BEGIN{
FS="\n"
RS=""
OFS=","
}
{
print $1,$2,$3
}

处理多于三个参数记录的情况

BEGIN{
FS="\n"
RS=""
ORS=""
}
{
x=1
while(x<NF){
print $x "\t"
x++
}
print $NF "\n"
}

 

if else
{
if($1=="foo"){
if($2=="foo"){
print "nuno"
}else{
print "one"
}
}else if($1=="bar"){
print "two"
}else{
print "three"
}
}

awk 'BEGIN{test=20;if(test>90){print "very good"}else if (test>60){print "good"}else {print "no pass"} }'

 

while
awk 'BEGIN{test=100;total=0;while(i<=test){total+=i;i++} print total}'

 

for
awk 'BEGIN{for (i in INVIRON) {print i"="INVIRON[i]}}'
awk 'BEGIN{total=0;for(i=0;i<=100;i++){total+=i} print total}'

 

do while
awk 'BEGIN{total=0;i=0; do{total+=i;i++}while(i<=100) print total}'

time(awk 'BEGIN{total=0;for(i=0;i<=10000;i++) {total+=i} print total}')
time(total=0;for i in $(seq 10000);do total=$(($total+i));done;echo $total)

 

xiong.sh
BEGIN{
array["1"]="xiong"
print array["1"]
}

BEGIN{
array[1]="xiong"
print array[1]
}

BEGIN{
array[name]="xiong"
print array[name]
}

BEGIN{
array["name"]="xiong"
delete array["name"]
print array["name"]
}

 

xiong.sh
BEGIN{
if(i in array){
print "yes"
}else{
print "no"
}
}

 

xiong.awk
BEGIN{
mysting="This is xiao xiongxiong"
print length(mystring)
}

BEGIN{
mystring="This is xiaoxiao"
pring index(mystring,"xiao")
}

BEGIN{
mystring="HOW Do YOU DO"
pring tolower(string)
}

BEGIN{
mysting="how do you do"
print toupper(string)
}

 

BEGIN{
mystring="xiongxiong is a dogxiong"
print substr(mystring,5,5)
}

一个是仅仅替换第一个匹配上的,一个是替换所有匹配上的
BEGIN{
mystring="xiongxiong is a dog xiong"
sub(/o/,"O",mystring)
print mystring
}
BEGIN{
mystring="xiongxiong is a dog xiong"
gsub(/o/,"O",mystring)
print mysting
}

 

BEGIN{
numelements=split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec",mymonths,",")
print mymonths[1],mymonths[nummonths]
}

posted @ 2021-04-02 11:18  daniel5  阅读(68)  评论(0)    收藏  举报