简体   繁体   English

带有awk的数据矩阵

[英]matrix from data with awk

Warning, not an awk programmer. 警告,不是awk程序员。

I have a file, let's call it file.txt. 我有一个文件,我们称它为file.txt。 It has a list of numbers which I will be using to find the information I need from the rest of the directory (which is full of files *.asc). 它有一个数字列表,我将用它来从目录的其余部分(包含* .asc文件)中查找所需的信息。 The remaining files do not have the same lengths, but since I will be drawing data based on file.txt, the matrix I will be building will have the same number of rows. 其余文件的长度不同,但是由于我将基于file.txt绘制数据,因此我将要构建的矩阵将具有相同的行数。 All files DO however contain the same number of columns, 3. The first column will be compared to file.txt, the second column of each *.asc file will be used to build the matrix. 但是,所有文件都包含相同数量的列3。第一列将与file.txt进行比较,每个* .asc文件的第二列将用于构建矩阵。 Here is what I have so far: 这是我到目前为止的内容:

awk '
    NR==FNR{
        A[$1];
        next}
    $1 in A
       {print $2 >> "data.txt";}' file.txt *.asc

This, however, prints the information from each file below the previous file. 但是,这会将每个文件中的信息打印到上一个文件的下方。 I want the information side by side, like a matrix. 我希望信息可以像矩阵一样并排放置。 I looked up paste, but it seems to be called before awk, and all examples were only of a couple of files. 我查找了粘贴,但似乎在awk之前被称为粘贴,所有示例仅包含几个文件。 I tried it still in place of print and did not work. 我仍然尝试用它代替印刷品,但是没有用。

If anyone could help me out, this would be the last piece to my project. 如果有人可以帮助我,这将是我项目的最后一部分。 Thanks so much! 非常感谢!

You could try: 您可以尝试:

awk -f ext.awk file.txt *.asc > data.txt

where ext.awk is ext.awk在哪里

NR==FNR {
    A[$1]++
    next
}
FNR==1 {
    if (ARGIND > 2)
        print ""
}
$1 in A {
    printf "%s ", $2
}
END {
    print ""
}

Update 更新

If you do not have Gnu Awk, the ARGIND variable is not available. 如果您没有Gnu Awk,则ARGIND变量不可用。 You could then try 然后您可以尝试

NR==FNR {
    A[$1]++
    next
}
FNR==1 {
    if (++ai > 1)
        print ""
}
$1 in A {
    printf "%s ", $2
}
END {
    print ""
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM