【问题标题】:Append one column from fileB onto end of FileA将文件中的一列附加到文件末尾
【发布时间】:2015-02-24 11:56:17
【问题描述】:

这类似于

How to add a column from a file to another file

但在这种情况下,我只想使用 5 列文件 B 中的第 5 列。两个制表符分隔,长度相同。有没有办法用 awk ,粘贴或其他东西来做到这一点?

file A
str str str 
str2 str2 str2

fileB
as at aw ay ao 
re rt ty yu ui

oFile
str str str ao
str2 str2 str2 ui

ExtraInfo,我正在创建一些类似的文件,我只想要其中的一列。示例完整脚本,obv 不正确;

 for pheno in $(seq 1 $nbGroups)
 do
    batch=$(sed -n $pheno'p' $Names)
    ## do some stuff to $batch

if [ $pheno=1 ] 
then
    awk '{ print $1, $1, $5}' $batch > $bDir"NewFile"
fi 
if [ $pheno>1 ] 
then
    awk '{ print $5}' $batch >> $bDir"File"
fi 

done

【问题讨论】:

  • 发布一个示例以及预期的输出。
  • 是的,谢谢。
  • 不清楚第二个 sn-p 与您的问题有何关系 - 没有明显的尝试将多个文件中的列组合在一起。
  • 我知道它不起作用。这就是我问一个问题的原因。没有任何明显的努力是粗鲁和不正确的。
  • 你真的在使用 bash 吗?如果是这样,数字测试应该是((pheno==1))((pheno>1))。如果您受限于 Posix shell 功能,则需要 [ $pheno -eq 1 ][ $pheno -lt 1 ]。内置的test(也拼写为[)保留=>用于字符串比较,并要求它们是单独的单词(即用空格包围;另外,因为<>是shell 元字符,它们需要被引用。所有这些都使得[[(( 构造更不容易出错。

标签: bash


【解决方案1】:

一种简单的方法,使用 bash 进程替换

 paste file1 <(cut -f5 file2)

没有 bash 扩展:

 cut -f5 file2 | paste file1 -

【讨论】:

  • 谢谢 rici。我选择了 cut 选项,因为带有 '
【解决方案2】:

你可以试试下面的。

$ paste -d'\t' file1 <(awk '{print $NF}' file2)
str str str  ao
str2 str2 str2 ui

$ paste -d'\t' file1 <(awk '{print $5}' file2)
str str str  ao
str2 str2 str2 ui

paste file1 <(awk '{print $5}' file2)

【讨论】:

  • 在 OP 中说“两个制表符分隔”,所以 paste -d' ' 不应该是 paste -d '\t' 甚至只是 paste file1 ... 作为 paste 的默认分隔符是一个制表符吗?
猜你喜欢
  • 1970-01-01
  • 2017-06-15
  • 1970-01-01
  • 1970-01-01
  • 2014-05-11
  • 2010-09-23
  • 2017-07-08
  • 2021-09-07
  • 2017-03-26
相关资源
最近更新 更多