保持分隔符处于awk输出中 [英] Maintaining the separator in awk output

查看:258
本文介绍了保持分隔符处于awk输出中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用bash中的"awk"将分隔符保留在子集输出中的同时对文件进行子集化.

I would like to subset a file while I keep the separator in the subsetted output using ´awk´ in bash.

这就是我正在使用的:

输入文件是用R语言创建的:

The input file is created in R language with:

inp <- 'AX-1   1    125  AA  0.2  1 AB -0.89 0 AA 0.005 0.56
        AX-2   2    456  AA  0   0 AA -0.56 0.56 AB -0.003 0
        AX-3   3    3445  BB  1.2  1 NA  0.002 0 AA 0.005 0.55'
inp <- read.table(text=inp, header=F)
write.table(inp, "inp.txt", col.names=F, row.names=F, quote=F, sep="\t")

(因此字段由制表符分隔)

bash中的代码:

awk {'print $1 $2 $3'} inp.txt

结果:

AX-11125

AX-11125

AX-22456

AX-333445

AX-333445

请注意,我的列已合并到awk输出中(我希望将其用制表符分隔为输入文件).可能这是一个简单的语法问题,但我将不胜感激.

Please note that my columns were merged in the awkoutput (and I would like it to be tab delimited as the input file). Probably it is a simple syntax problem, but I would be grateful to any ideas.

推荐答案

使用

awk -v OFS='\t' '{ print $1, $2, $3 }'

awk '{ print $1 "\t" $2 "\t" $3 }'

一个接一个地写着,但它们之间没有运算符,awk中的变量被连接起来-在这方面$1 $2 $3$1$2$3没什么不同.

Written one after another without an operator between them, variables in awk are concatenated - $1 $2 $3 is no different from $1$2$3 in this respect.

第一个解决方案将输出字段分隔符OFS设置为选项卡,然后使用逗号运算符来打印分隔的字段.第二种解决方案只是直接在其中散布标签,所有内容都像以前那样串联在一起.

The first solution sets the output field separator OFS to a tab, then uses the comma operator to print separated fields. The second solution simply sprinkles tabs in there directly, and everything is concatenated as it was before.

这篇关于保持分隔符处于awk输出中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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