Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 将列追加到另一个文本文件中_Linux_Bash_For Loop_Awk - Fatal编程技术网

Linux 将列追加到另一个文本文件中

Linux 将列追加到另一个文本文件中,linux,bash,for-loop,awk,Linux,Bash,For Loop,Awk,Hii我有一个3列的文本文件 2.0 44.8 789.3 3.0 58.4 453.0 4.0 97.2 -489.1 5.2 35.3 458.6 我想从上述文本文件中选择列,并想将所选列附加到另一个文本文件中。下面给出了我要附加上述列的文件 > > > > 10.0 8.5 20.0 8.5 30.0 8.5 40.0 8.5 > > > > 10.0

Hii我有一个3列的文本文件

2.0  44.8  789.3 
3.0  58.4  453.0
4.0  97.2 -489.1
5.2  35.3  458.6
我想从上述文本文件中选择列,并想将所选列附加到另一个文本文件中。下面给出了我要附加上述列的文件

> > > > 
    10.0    8.5
    20.0    8.5
    30.0    8.5
    40.0    8.5
> > > >
    10.0    8.0
    20.0    8.0
    30.0    8.0
    40.0    8.0
> > > >
    10.0    9.0
    20.0    9.0
    30.0    9.0
    40.0    9.0
> > > >
我的预期产出是

> > > > 
    10.0    8.5   2.0
    20.0    8.5   3.0
    30.0    8.5   4.0
    40.0    8.5   5.2
> > > >
    10.0    8.0   44.8
    20.0    8.0   58.4
    30.0    8.0   97.2
    40.0    8.0   35.3
> > > >
    10.0    9.0   789.3
    20.0    9.0   453.0
    30.0    9.0  -489.1
    40.0    9.0   458.6
> > > >
我尝试了剧本,但之后没有得到更多的想法,我需要专家的帮助。提前谢谢

 #!/bin/sh
 for file in inp.txt
 do
 awk '{print $1}' > colone
 done

根据OP公司展示的样品,请您尝试以下内容。这将在文本文件中第三次出现>>后打印计数的重置值,并从输入文件的第1列值开始再次开始打印

awk '
FNR==NR{
  for(i=1;i<=NF;i++){
    value[FNR,i]=$i
  }
  next
}
/^> >/{
  count=0
  print
  if(col==3){ col=0 }
  col++
  next
}
{
  print $0"    "value[++count,col]
}
' Input_file text_file
说明:增加对以上内容的详细说明

awk '                       ##Starting awk program from here.
FNR==NR{                    ##Checking condition FNR==NR when Input_file is being read.
  for(i=1;i<=NF;i++){       ##Traversing through all fields here. 
    value[FNR,i]=$i         ##Creating value with index of FNR,i here with value $i
  }
  next                      ##next will skip all further statments from here.
}
/^> >/{                     ##Checking condition if line starts from > > then do following.
  count=0                   ##Nulliffying count here.
  print                     ##Printing current line.
  if(col==3){ col=0 }       ##Checking if col is 3 then make its value 0 here. Why because OP sample has only 3 blocks and
                            ##if there are more than 3 then it will start printing from very 1st values onwards after every 3 blocks.
  col++                     ##Increasing value of col with 1 here.
  next                      ##next will skip all further statments from here.
}
{
  print $0"    "value[++count,col]  ##Printing current line with space and array value here.
}
' Input_file text_file      ##Mentioning Input_file and text_file names here.

大家好,欢迎来到SO。您的脚本有一些语法错误,请检查。欢迎使用SO。很好,你在问题中展示了你的尝试。为了更好地理解你的问题,请你解释一下在新文件中添加列的逻辑。是的,为了我的工作目的,我正在尝试这样做it@lijun,正如前面的评论中所提到的,您是否可以添加如何在哪些行或行号上附加columneg的逻辑?是的,第一列将被附加到第一个块上,然后第二列将被附加到第二个块上等等。它工作正常,但是我们可以在这些块之间添加更多的空间吗columns@lijun,我相信你的意思是更少的空间,我现在已经修复了它,我们应该得到与文本文件相同的空间格式的输出。请一定要让我知道它现在怎么样了?你的脚本可以工作吗,如果我有更多的文本列file@lijun,我不这样认为,答案将根据问题给出。所以现在请不要改变你的基本问题。你可以用你给定的样本来测试这个答案,也可以试着玩弄它,如果你有问题,那么你可以在论坛上用你的努力来创建一个新的问题。但是,不建议更改基本问题的要求,所以,干杯。请您建议在您的脚本中的何处提供输入?显示在何处提供输入-awk-f tst.awk文件1文件2。如果您不想将awk脚本保存在文件中,可以将其放在命令行awk'script'file1 file2的单引号之间。
$ cat tst.awk
NR==FNR {
    for (numBlocks=1; numBlocks<=NF; numBlocks++) {
        vals[numBlocks,NR] = $numBlocks
    }
    next
}
/^>/ {
    blockNr++
    rowNr = 0
    print
    next
}
{ printf "%s %7s\n", $0, vals[blockNr,++rowNr] }
$ awk -f tst.awk file1 file2
> > > >
    10.0    8.5     2.0
    20.0    8.5     3.0
    30.0    8.5     4.0
    40.0    8.5     5.2
> > > >
    10.0    8.0    44.8
    20.0    8.0    58.4
    30.0    8.0    97.2
    40.0    8.0    35.3
> > > >
    10.0    9.0   789.3
    20.0    9.0   453.0
    30.0    9.0  -489.1
    40.0    9.0   458.6
> > > >