我有这样一个.txt文件:
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“文件如下所示:
col1 col2
1 3
2 4
3 1
5 3我试过这样的代码:
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“?
编辑:上面的列名只是一个例子。我更喜欢使用提取列名标题的命令,因为实际上我的文件使用不同的单词作为标题。
发布于 2021-01-28 17:20:43
这里有一个类似的..。
$ 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发布于 2021-01-28 17:10:08
根据您所展示的样品,请您试一试。用GNU awk中显示的示例编写。
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.txt、col3.reform.txt、col4.reform.txt等名称创建。
col2.reform.txt内容的样本如下:
cat col2.reform.txt
col1 col2
1 3
2 4
3 1
5 3解释:添加了上面的详细说明。
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.发布于 2021-01-28 18:43:16
$ 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测试完成后,只需将" > "更改为>,将" >> "更改为>>,并希望实际生成输出文件。
https://stackoverflow.com/questions/65941781
复制相似问题