2013-09-29 52 views
1

我需要扭轉包含文本文件的字段:沖銷場與AWK

Computer science is fun 
I also enjoy math 
But I don't like science 

我需要awk到輸出:

line 1: Computer science is fun 
line 1 reversed: fun is science Computer 
line 2: I also enjoy math 
line 2 reversed: math enjoy also I 

.... ... 等等。 這就是我現在所擁有的:

BEGIN {print "start"} 
{ 
    for(i=NF; i>=1; i--) 
    { 
     printf "line %d: %s\n", NR , $0; 
     printf "line %d: reversed: %s", NR, $i; 
    } 
} 

回答

2

你接近:

BEGIN {print "start"} 
{ 
    printf "line %d: %s\n", NR , $0 
    printf "line %d: reversed:", NR 
    for(i=NF; i>=1; i--) 
    { 
     printf " %s", $i 
    } 
    print "" 
} 

以上是假設你不關心領域之間的空白。

+1

太棒了!謝謝! – mrplow911

1

嘗試:

awk '{for (i=NF; i>0; i--) printf("%s ",$i);print ""}' input.txt