剪切/编辑行线,然后将行转置为列数组

发布于 2025-01-11 15:16:34 字数 760 浏览 0 评论 0原文

我极大地简化了问题中的值

我在 space-delim 文件中有 500k 行文本,我需要提取 x, y 值以从每一行进行绘图并将这些行转置为列。我正在尝试确定最有效的方法,并且我一直在使用 sed 和 awk 编写 bash 脚本 - 但如果有更好的方法这样做的方法,我愿意接受建议。

以下代码块是前 3 行文本的简单示例。前 4 个值(a、b、c、num)可以忽略,但随后每对值(以 0.000 分隔)需要被剪切并放入一列中,我需要对每一行执行此操作。 2、3 或 2(紧接在 c 之后)告诉我们该行中有多少个 x,y 对,以 2500 和 < 开头code>500

示例输入:

a   b   c   2   2500.0  500.0   0.000   0.0 10.0            
a   b   c   3   2000.0  450.0   0.000   1000.0  400.0   0.000   0.0 12.0
a   b   c   2   1800.0  475.0   0.000   0.0 15.0

预期输出:

2500.0  500.0   2000.0  450.0   1800.0  475.0
0.0     10.0    1000.0  400.0   0.0     15.0
                0.0     12.0

I've greatly simplified the values in the question

I have 500k lines of text in a space-delim file that I need to extract x, y values for plotting from each row and transpose those rows to columns. I'm trying to determine the most efficient way of doing this, and I've most been working in a bash script using sed and awk -- but if there are better ways of doing this, I'm open to suggestions.

The following code block is a simple example of the first 3 lines of text. The first 4 values (a,b,c,num) can be ignored but then each pair of values (separated by 0.000) needs to be cut and placed into a column and I need to do this for every line. The 2, 3 or 2 (right after c) tells us how many x,y pairs follow in the line, starting with 2500 and 500

Sample input:

a   b   c   2   2500.0  500.0   0.000   0.0 10.0            
a   b   c   3   2000.0  450.0   0.000   1000.0  400.0   0.000   0.0 12.0
a   b   c   2   1800.0  475.0   0.000   0.0 15.0

Expected output:

2500.0  500.0   2000.0  450.0   1800.0  475.0
0.0     10.0    1000.0  400.0   0.0     15.0
                0.0     12.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘逸的'云 2025-01-18 15:16:34

我突然灵光一闪——这就是你想做的事情吗:

$ cat tst.awk
BEGIN { OFS="\t" }
{
    rowNr = 0
    for ( i=6; i<NF; i+=3 ) {
        val = $i OFS $(i+1)
        vals[++rowNr,NR] = val
    }
    numRows = ( rowNr > numRows ? rowNr : numRows )
}
END {
    numCols = NR
    for ( rowNr=1; rowNr<=numRows; rowNr++ ) {
        for ( colNr=1; colNr<=numCols; colNr++ ) {
            val = ( (rowNr,colNr) in vals ? vals[rowNr,colNr] : OFS )
            printf "%s%s", val, (colNr<numCols ? OFS : ORS)
        }
    }
}

$ awk -f tst.awk file
2500.0  500.0   2000.0  450.0   1800.0  475.0
0.0     10.0    1000.0  400.0   0.0     15.0
                0.0     12.0

以前的答案:

使用您的新示例:

$ awk '{for (i=6;i<NF;i+=3) print $i, $(i+1)}' file
2500.0 500.0
0.0 10.0
2000.0 450.0
1000.0 400.0
0.0 12.0
1800.0 475.0
0.0 15.0

原始答案:

$ awk '{for (i=5;i<NF;i+=3) print $i, $(i+1)}' file
2328.948975 287.601868
2091.017578 168.763153
1949.201782 38.351677
1926.619385 43.498230
1808.323120 91.477585
1792.807861 60.784626
1650.532471 25.608397
1630.271484 54.119873
1615.212891 4.026413
1502.170288 118.276688
1176.283447 43.057251
1119.772705 56.983471
944.500000 81.461624
937.491516 107.800484
726.215515 181.033768
641.585510 82.066551
443.907104 27.303604
362.327789 90.082993
348.752777 39.833252
61.434803 44.582367
0.000000 7.181455

I had a flash of inspiration - is this what you're trying to do:

$ cat tst.awk
BEGIN { OFS="\t" }
{
    rowNr = 0
    for ( i=6; i<NF; i+=3 ) {
        val = $i OFS $(i+1)
        vals[++rowNr,NR] = val
    }
    numRows = ( rowNr > numRows ? rowNr : numRows )
}
END {
    numCols = NR
    for ( rowNr=1; rowNr<=numRows; rowNr++ ) {
        for ( colNr=1; colNr<=numCols; colNr++ ) {
            val = ( (rowNr,colNr) in vals ? vals[rowNr,colNr] : OFS )
            printf "%s%s", val, (colNr<numCols ? OFS : ORS)
        }
    }
}

$ awk -f tst.awk file
2500.0  500.0   2000.0  450.0   1800.0  475.0
0.0     10.0    1000.0  400.0   0.0     15.0
                0.0     12.0

Previous answers:

Using your new example:

$ awk '{for (i=6;i<NF;i+=3) print $i, $(i+1)}' file
2500.0 500.0
0.0 10.0
2000.0 450.0
1000.0 400.0
0.0 12.0
1800.0 475.0
0.0 15.0

Original answer:

$ awk '{for (i=5;i<NF;i+=3) print $i, $(i+1)}' file
2328.948975 287.601868
2091.017578 168.763153
1949.201782 38.351677
1926.619385 43.498230
1808.323120 91.477585
1792.807861 60.784626
1650.532471 25.608397
1630.271484 54.119873
1615.212891 4.026413
1502.170288 118.276688
1176.283447 43.057251
1119.772705 56.983471
944.500000 81.461624
937.491516 107.800484
726.215515 181.033768
641.585510 82.066551
443.907104 27.303604
362.327789 90.082993
348.752777 39.833252
61.434803 44.582367
0.000000 7.181455
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文