首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >提取列并输出由列标题命名的文件。

提取列并输出由列标题命名的文件。
EN

Stack Overflow用户
提问于 2021-01-28 17:00:41
回答 3查看 236关注 0票数 2

我有这样一个.txt文件:

代码语言:javascript
复制
col1    col2    col3    col4
1   3   4   A
2   4   6   B
3   1   5   D
5   3   7   F

我希望提取列1后面的每一列( i ),并将column1和列i输出到第一列标题命名的新文件中。

这意味着我将有三个输出文件,分别名为"col2.reform.txt“、"col3.reform.txt”和"col4.reform.txt“。

例如,输出"col2.reform.txt“文件如下所示:

代码语言:javascript
复制
col1    col2
1   3
2   4
3   1
5   3

我试过这样的代码:

代码语言:javascript
复制
awk '{for (i=1; i <=NF; i++) print $1"\t"$i > ("{awk 'NR==1' $i}"".reform.txt")}' inputfile

显然,"{awk 'NR==1‘$i}“部分不能工作,我得到了一个名为{awk 'NR==1’$i}.reform.txt的文件。

如何才能正确地获得文件名?谢谢!

PS:我如何删除终端中的文件"{awk 'NR==1‘$i}.reform.txt“?

编辑:上面的列名只是一个例子。我更喜欢使用提取列名标题的命令,因为实际上我的文件使用不同的单词作为标题。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-01-28 17:20:43

这里有一个类似的..。

代码语言:javascript
复制
$ awk 'NR==1 {n=split($0,h)} 
             {for(i=2;i<=n;i++) print $1,$i > (h[i]".reform.txt")}' file

==> col2.reform.txt <==
col1 col2
1 3
2 4
3 1
5 3

==> col3.reform.txt <==
col1 col3
1 4
2 6
3 5
5 7

==> col4.reform.txt <==
col1 col4
1 A
2 B
3 D
5 F
票数 2
EN

Stack Overflow用户

发布于 2021-01-28 17:10:08

根据您所展示的样品,请您试一试。用GNU awk中显示的示例编写。

代码语言:javascript
复制
awk '
FNR==1{
  for(i=1;i<=NF;i++){
    heading[i]=$i
  }
  next
}
{
  for(i=2;i<=NF;i++){
    close(outFile)
    outFile="col"i".reform.txt"
    if(!indVal[i]++){ print heading[1],heading[i] > (outFile) }   
    print $1,$i >> (outFile)
  }
}
' Input_file

输出文件将以例如-> col2.reform.txtcol3.reform.txtcol4.reform.txt等名称创建。

col2.reform.txt内容的样本如下:

代码语言:javascript
复制
cat col2.reform.txt
col1 col2
1 3
2 4
3 1
5 3

解释:添加了上面的详细说明。

代码语言:javascript
复制
awk '                             ##Starting awk program from here.
FNR==1{                           ##Checking condition if this is first line then do following.
  for(i=1;i<=NF;i++){             ##Traversing through all fields of current line.
    heading[i]=$i                 ##Creating heading array with index of i and value of current field.
  }
  next                            ##next will skip all further statements from here.
}
{
  for(i=2;i<=NF;i++){             ##Traversing from 2nd field to till last field of all rest of lines.
    close(outFile)                ##Closing outFile to avoid too many opened files error.
    outFile="col"i".reform.txt"   ##Creating outFile which has output file name in it.
    if(!indVal[i]++){ print heading[1],heading[i] > (outFile) }   
                                  ##Checking condition if i is NOT present in indVal then print 1st element of heading and current element of heading into outFile.
    print $1,$i >> (outFile)      ##Printing 1st field and current field values to output file here.
  }
}
' Input_file                      ##Mentioning Input_file name here.
票数 5
EN

Stack Overflow用户

发布于 2021-01-28 18:43:16

代码语言:javascript
复制
$ awk '
    NR==1 { split($0,hdrs) }
    {
        for (i=2; i<=NF; i++) {
            out = hdrs[i]".reform.txt"
            if (FNR==1) {
                printf "" " > " out    # to erase exiting file contents if any
            }
            print $1, $i " >> " out
            close(out)
        }
    }
' file
 > col2.reform.txtcol1 col2 >> col2.reform.txt
 > col3.reform.txtcol1 col3 >> col3.reform.txt
 > col4.reform.txtcol1 col4 >> col4.reform.txt
1 3 >> col2.reform.txt
1 4 >> col3.reform.txt
1 A >> col4.reform.txt
2 4 >> col2.reform.txt
2 6 >> col3.reform.txt
2 B >> col4.reform.txt
3 1 >> col2.reform.txt
3 5 >> col3.reform.txt
3 D >> col4.reform.txt
5 3 >> col2.reform.txt
5 7 >> col3.reform.txt
5 F >> col4.reform.txt

测试完成后,只需将" > "更改为>,将" >> "更改为>>,并希望实际生成输出文件。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65941781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档