Ubuntu 将.txt文件拆分为单独的文件,从一列中提取名称,从另一列中提取内容?

Ubuntu 将.txt文件拆分为单独的文件,从一列中提取名称,从另一列中提取内容?,ubuntu,awk,Ubuntu,Awk,如果已经有人问过,我道歉。我环顾四周,只能找到对我不起作用的类似问题 我有一个以制表符分隔的.txt文件,其中有两列要拆分。我需要新文件的名称基于第一列,并且每个文件的内容都是第二列的内容。例如,我将拥有Name1.txt,其中包含字符串word1,另一个名为Name2.txt的文件,其中包含word2,以此类推。我使用的是Ubuntu18.04,我希望这可以通过命令行完成 Name1 word1 Name2 word2 Name3 word3 目前,我已使用以下代码将.txt文件

如果已经有人问过,我道歉。我环顾四周,只能找到对我不起作用的类似问题

我有一个以制表符分隔的.txt文件,其中有两列要拆分。我需要新文件的名称基于第一列,并且每个文件的内容都是第二列的内容。例如,我将拥有
Name1.txt
,其中包含字符串
word1
,另一个名为
Name2.txt
的文件,其中包含
word2
,以此类推。我使用的是Ubuntu18.04,我希望这可以通过命令行完成

Name1   word1
Name2   word2
Name3   word3
目前,我已使用以下代码将.txt文件拆分为单独的文件:

split -1 largefile.txt
现在,每个文件的格式都相同。对于字符串,我希望首先显示名称,然后是选项卡和所需的文件内容

Name1   word1 

你能试试下面的吗

awk '
{
  outfile=$1
  if(outfile!=prev){
    close(outfile)
  }
  print $2 > (outfile".txt")
  prev=$1
}
' Input_file
解释:添加详细解释

awk '                           ##Starting awk program from here.
{
  outfile=$1                    ##Creating variable oufile which stores first field of current line.
  if(outfile!=prev){            ##Checking condition if outfile value is NOT equal to prev variable then do following.
    close(outfile)              ##Then closing outfile(output file) in backend, this step is to avoid error of too many opened files.
  }
  print $2 > (outfile".txt")    ##Printing current line 2nd field to outfile with .txt adding to it.
  prev=$1                       ##Creating variable prev which has 1st field value of current line.
}
' Input_file                    ##Mentioning Input_file name here.
另一个awk:

$ awk '{f=$1 ".txt";print $2 >> f;close(f)}' file
解释:

$ awk '{
    f=$1 ".txt"     # form the filename
    print $2 >> f   # append to file in case there are non-unique $1s
    close(f)        # close file to avoid running out of fds
}' file

因此,我们鼓励用户添加他们为解决自己的问题所付出的努力,因此请在您的问题中添加同样的努力,然后让我们知道。我知道,我已经用我到目前为止所做的编辑了这篇文章。这正确地分割了文件,但在文件中留下了成为名称的字符串。@sjp,好的,我现在只打印第二行字段。让我知道这现在是否行得通?