我有一个平面文件,我想从所有行中解析奇数列。
我是UNIX的新手,我接触的方式就是这个
awk 'BEGIN{ FS = OFS = "|" } { for (i=2;i<=NF;i++) { if(i%2==0) { print $i }}}' newProcessFile.txt
显然它没有产生所需的输出。 我究竟做错了什么 ?请解释 。
答案 0 :(得分:3)
您可以执行此操作awk
awk '{for (i=1;i<=NF;i+=2) printf "%s ",$i;print ""}' file
它会打印每隔一列。
cat file
one
one two
one two three
one two three four
one two three four five
one two three four five six
awk '{for (i=1;i<=NF;i+=2) printf "%s ",$i;print ""}' file
one
one
one three
one three
one three five
one three five
使用其他分隔符:
cat file
one
one|two
one|two|three
one|two|three|four
one|two|three|four|five
one|two|three|four|five|six
awk -F\| '{s="";for (i=1;i<=NF;i+=2) {s=s?s FS $i:$i} print s}' file
one
one
one|three
one|three
one|three|five
one|three|five