Shell 使用awk转置多个文件中的值
我有10个文件。例如:Shell 使用awk转置多个文件中的值,shell,awk,Shell,Awk,我有10个文件。例如: $ cat ifile01.txt 1 0.22 2 0.01 4 0.32 5 0.10 . . $ cat ifile02.txt 1 0.23 2 0.11 3 0.12 4 0.20 . . $ cat ifile03.txt 1 0.32 2 0.64 3 0.12 5 0.90 . . for (i=1;i<=cols;i++) printf "%4s%s", a[i], (i<cols?" ":ORS) 等10个文
$ cat ifile01.txt
1 0.22
2 0.01
4 0.32
5 0.10
. .
$ cat ifile02.txt
1 0.23
2 0.11
3 0.12
4 0.20
. .
$ cat ifile03.txt
1 0.32
2 0.64
3 0.12
5 0.90
. .
for (i=1;i<=cols;i++) printf "%4s%s", a[i], (i<cols?" ":ORS)
等10个文件
这里第一列是序列号,第二列是它们的索引
我希望在一个文件中以以下方式重新排列它们:
outfile.txt
0.22 0.01 ? 0.32 0.10 (Transpose of the index from ifile01.txt with "?" for serial number 3, as the index for 3 is missing)
0.23 0.11 0.12 0.20 ? (Transpose of the index from ifile02.txt with "?" for serial number 5, as the index for 5 is missing)
0.32 0.64 0.12 ? 0.90 (Transpose of the index from ifile03.txt with "?" for serial number 4, as the index for 4 is missing)
我在fortran中尝试以下方法,但我正在寻找一个awk脚本
for each ifile.txt, I check the follwoing
for i in {1..50};do
if [ $i != $1 ]; then i="?"
Then append print transpose of $2 for each ifile.txt in outfile.txt
你能试试下面的吗
awk '
FNR==1{
count++
}
{
a[count,$1]=$2
if(!c[$1]++){
d[++occ]=$1
}
}
END{
asorti(d,e)
for(i=1;i<=count;i++){
for(k=1;k<=occ;k++){
printf("%s ",a[i,e[k]]?a[i,e[k]]:"?")
}
print ""
}
}
' Input_file1 Input_file2 Input_file3 | column -t
解释:为上述代码添加解释
awk ' ##Starting awk program from here.
FNR==1{ ##Checking condition if this is first line of Input_file.
count++ ##increment variable count value with 1 here.
}
{
a[count,$1]=$2 ##Creating an array named a with index count and $1 whose value is $2 of current line.
if(!c[$1]++){ ##Checking condition if $1 is NOT present in array c then do following.
d[++occ]=$1 ##Creating an array named d whose index is occ variable and value is $1 of current line.
} ##Closing BLOCK for if condition.
} ##Closing main BLOCK.
END{ ##starting END block for this awk program here.
asorti(d,e) ##Using asorti to sort array d and creating array e with it(which has sorted values in it).
for(i=1;i<=count;i++){ ##Starting a for loop from i=1 to till value of count(number of files actually).
for(k=1;k<=occ;k++){ ##Starting a for loop from k=1 to till value of occ.
printf("%s ",a[i,e[k]]?a[i,e[k]]:"?") ##Printing value of array a whose index is variable i AND array e with index of k if its NOT NULL else print ? as per OP
} ##Closing BLOCK for, for Loop here.
print "" ##Printing NULL value to get a new line here.
} ##Closing BLOCK for outer for loop here.
} ##Closing BLOCK for END block of this awk program here.
' file1 file2 file3 | column -t ##Mentioning Input_file names here and using colunm -t to put equal spacing in their output.
awk'##从这里启动awk程序。
FNR==1{##如果这是输入文件的第一行,则检查条件。
count++##这里用1递增变量计数值。
}
{
a[count,$1]=$2##创建一个名为a的数组,该数组具有索引计数和$1,其值为当前行的$2。
如果(!c[$1]++){##检查条件如果数组c中不存在$1,则执行以下操作。
d[++occ]=$1##创建一个名为d的数组,其索引为occ变量,值为当前行的$1。
}##关闭if条件的块。
}##关闭主块。
结束{##这里是这个awk程序的起始结束块。
asorti(d,e)##使用asorti对数组d进行排序,并使用它创建数组e(其中包含已排序的值)。
对于(i=1;i你能试试下面的吗
awk '
FNR==1{
count++
}
{
a[count,$1]=$2
if(!c[$1]++){
d[++occ]=$1
}
}
END{
asorti(d,e)
for(i=1;i<=count;i++){
for(k=1;k<=occ;k++){
printf("%s ",a[i,e[k]]?a[i,e[k]]:"?")
}
print ""
}
}
' Input_file1 Input_file2 Input_file3 | column -t
解释:为上述代码添加解释
awk ' ##Starting awk program from here.
FNR==1{ ##Checking condition if this is first line of Input_file.
count++ ##increment variable count value with 1 here.
}
{
a[count,$1]=$2 ##Creating an array named a with index count and $1 whose value is $2 of current line.
if(!c[$1]++){ ##Checking condition if $1 is NOT present in array c then do following.
d[++occ]=$1 ##Creating an array named d whose index is occ variable and value is $1 of current line.
} ##Closing BLOCK for if condition.
} ##Closing main BLOCK.
END{ ##starting END block for this awk program here.
asorti(d,e) ##Using asorti to sort array d and creating array e with it(which has sorted values in it).
for(i=1;i<=count;i++){ ##Starting a for loop from i=1 to till value of count(number of files actually).
for(k=1;k<=occ;k++){ ##Starting a for loop from k=1 to till value of occ.
printf("%s ",a[i,e[k]]?a[i,e[k]]:"?") ##Printing value of array a whose index is variable i AND array e with index of k if its NOT NULL else print ? as per OP
} ##Closing BLOCK for, for Loop here.
print "" ##Printing NULL value to get a new line here.
} ##Closing BLOCK for outer for loop here.
} ##Closing BLOCK for END block of this awk program here.
' file1 file2 file3 | column -t ##Mentioning Input_file names here and using colunm -t to put equal spacing in their output.
awk'##从这里启动awk程序。
FNR==1{##如果这是输入文件的第一行,则检查条件。
count++##这里用1递增变量计数值。
}
{
a[count,$1]=$2##创建一个名为a的数组,该数组具有索引计数和$1,其值为当前行的$2。
如果(!c[$1]++){##检查条件如果数组c中不存在$1,则执行以下操作。
d[++occ]=$1##创建一个名为d的数组,其索引为occ变量,值为当前行的$1。
}##关闭if条件的块。
}##关闭主块。
结束{##这里是这个awk程序的起始结束块。
asorti(d,e)##使用asorti对数组d进行排序,并使用它创建数组e(其中包含已排序的值)。
对于(i=1;i假设序列号以升序出现在文件中:
#指定最大值或通过预传递计算
awk-v cols=$(awk'm假设序列号以升序出现在文件中:
#指定最大值或通过预传递计算
awk-v cols=$(awk'mWhy notif(occ@jhnc,谢谢你让我知道。很抱歉我尝试了我的代码,但它不起作用。请随意添加我的代码编辑版本的你方,干杯,伙计:)我是说我的if
来取代你的。awk'FNR==1{f++}{a[f,$1]=2}m@jhnc,当然谢谢你让我知道,现在添加,干杯。@RavinderSingh13:谢谢你的帮助。如果每个文件最多有9行,你的脚本就可以工作。也许我们需要设置一些格式,以便它可以打印第1,2,3,…10,11,…20,21,…行,而不是1,10,11,12…,2,20,21,…编辑过的jhnc脚本工作得很好。为什么otif(occ@jhnc,谢谢你让我知道。很抱歉我尝试了我的代码,但它不起作用。请随意添加我的代码编辑版本的你方,干杯,伙计:)我是说我的if
来取代你的。awk'FNR==1{f++}{a[f,$1]=2}m@jhnc,当然谢谢你让我知道,现在添加,干杯。@RavinderSingh13:谢谢你的帮助。如果每个文件最多有9行,你的脚本就可以工作。也许我们需要设置一些格式,以便它可以打印第1,2,3,…10,11,…20,21,…行,而不是1,10,11,12…,2,20,21…。编辑过的jhnc脚本工作得非常好。。