打印与AWK所有字段通过OFS分离 [英] Print all Fields with AWK separated by OFS

查看:96
本文介绍了打印与AWK所有字段通过OFS分离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来打印由OFS分隔的所有记录,而不打字每列数。

 语法#Desired风格,令人失望的结果
[kbrandt @林间空地:〜]回声1 2 3 4| GAWK'BEGIN {OFS =:-(} {打印$ 0}
1 2 3 4#Desired结果,意外语法
[kbrandt @林间空地:〜]回声1 2 3 4| GAWK'BEGIN {OFS =:-)}; {打印$ 1,$ 2,$ 3,$ 4}'
1 :-) :-) 2 3 4 :-)


解决方案

这是第一个风格的变化:

 回声1 2 3 4| GAWK'BEGIN {OFS =:-(}; {$ 1 = $ 1;打印$ 0}

结果:

  1 :-( :-( 2 3 4 :-(

说明:

$ 1 = $ 1 是重建记录,使用当前OFS(你也可以看到<一个href=\"http://www.gnu.org/software/gawk/manual/gawk.html#Changing-Fields\">http://www.gnu.org/software/gawk/manual/gawk.html#Changing-Fields)

更新:

(由@EdMorton和@steve建议)这是一个简短的,awk命令中,设定OFS在命令行中的等价版本,并采取的优势,打印$ 1,0 作为默认操作:

 的awk -v OFS =:-('{$ 1 = $ 1} 1'

Is there a way to print all records separated by the OFS without typing out each column number.

#Desired style of syntax, undesired result
[kbrandt@glade: ~] echo "1 2 3 4" | gawk 'BEGIN { OFS=" :-( "}; {print $0}'        
1 2 3 4

#Desired result, undesired syntax
[kbrandt@glade: ~] echo "1 2 3 4" | gawk 'BEGIN { OFS=" :-) "}; {print $1,$2,$3,$4}'
1 :-) 2 :-) 3 :-) 4

解决方案

This is a variation on the first style:

echo "1 2 3 4" | gawk 'BEGIN { OFS=" :-( "}; {$1=$1; print $0}'

Results:

1 :-( 2 :-( 3 :-( 4

Explanation:

the $1=$1 is to rebuild the record, using the current OFS (you can also see http://www.gnu.org/software/gawk/manual/gawk.html#Changing-Fields)

Update:

(suggested by @EdMorton and @steve) This is a briefer, equivalent version of the awk command, that sets OFS in the command line, and takes advantage of print $0 as the default action:

awk -v OFS=" :-( " '{$1=$1}1'

这篇关于打印与AWK所有字段通过OFS分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆