AWK經典處理---來自The Awk Programming Language

一、Print the total number of input lines:ci

END {print NR}input

二、 Print the tenth input line:it

NR==10ast

三、Print the last field of the last input line:co

 {print $NF}printf

四、Print the last field of the last input line:ab

{field=$NF} END {print field} 或者 END {print $NF}

五、Print every input line with more than four fields:

NF>4

六、Print every input line in which the last field is more than 4:

$NF>4

七、Print the total number of fields in all input lines:

{nf=nf+NF} END {print nf}

八、Print the total number of lines that contain Beth:

/Beth/ {nlines=nlines+1} END{print nlines}

九、Print the largest first field and the line that contains it (assumes some $1 is positive):

$2>max {max=$2;maxline=$0} END {print max,maxline}

十、Print every line that has at lease one field:

NF>0

十一、Print every line long than 80 characters:

length($0)>80

十二、Print the number of fields in every line followed by the line itself:

{print NF,$0}

1三、Print the first two fields,in opposite order,of every line:

{print $2,$1}

1四、Exchange the first two fields of every line and then print the line:

{temp=$1;$1=$2;$2=temp;print}

1五、Print every line with the first field field replaced by the line number:

{$1=NR;print}

1六、Print every line after erasing the second field:

{$2="";print}

1七、Print in reverse order the fields of every line:

{for (i=NF;i>0;i=i-1)printf("%s",$i);printf("\n")}

1八、Print the sums of the fields of every line:

{sum=0;for(i=1;i<=NF;i=i+1) sum=sum+$i;print sum}

1九、Add up all fields in all lines and print the sum:

{for(i=1;i<=NF;i=i+1) sum=sum+$i} END{print sum}

20、Print every line after replacing each field by its absolute value:

{for(i=1;i<=NF;i=i+1) if($i<0) $i=-$i;print}

相關文章
相關標籤/搜索