Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix 在awk中操作列_Unix_Awk_Grep - Fatal编程技术网

Unix 在awk中操作列

Unix 在awk中操作列,unix,awk,grep,Unix,Awk,Grep,我想操纵一些冒号,以便更好地将一些数据表示为图形。 到目前为止,所有的数据都是这样排列的,第一行是标题,接下来的两行是x,然后是y数据。文件中的所有数字由一个选项卡分隔。像这样: 200.023 468.865 567.976 647.711 ##this line is the 'header' 59.927 58.099 60.546 61.461 ##this line is x data 1576.77 2192.02 1630.22 1879.04 ##thi

我想操纵一些冒号,以便更好地将一些数据表示为图形。 到目前为止,所有的数据都是这样排列的,第一行是标题,接下来的两行是x,然后是y数据。文件中的所有数字由一个选项卡分隔。像这样:

200.023 468.865 567.976 647.711    ##this line is the 'header'
59.927  58.099  60.546  61.461     ##this line is x data
1576.77 2192.02 1630.22 1879.04    ##this line is y data
59.769  59.300  60.380  61.308     ##next x
1111.36 2674.2  1590.89 970.134    ##next y
##numbers continue down, and across the page
我想取y坐标列1第3行,并将其放置在与x坐标列2第2行相同的行中,用制表符分隔,因此该行现在将是原来的两倍长。为了使标题行不偏离位置,我想我应该添加一些易于识别的内容,比如“space/t”,但任何东西都可以。 输出:

如果我需要澄清什么,请告诉我。
谢谢

这是一个awk单行程序:

$ awk 'NR==1{gsub("\t","\tspace\t");print;next}!(NR%2){split($0,a,"\t")}NR%2{for (i=1;i<=NF;i++) printf "%s\t%s\t", a[i], $i;print ""} ' file 
200.03  space   468.865 space   567.976 space   647.711
59.927  1576.77 58.099  2192.02 60.546  1630.22 61.461  1879.04 
59.769  1111.36 59.300  2674.2  60.380  1590.89 61.308  970.134 
或以更可读的格式:

$ awk '
    NR==1{
        gsub("\t","\tspace\t")
        print
        next
    }
    !(NR%2){
        split($0,a,"\t")
    }
    NR%2{
        for (i=1;i<=NF;i++) 
            printf "%s\t%s\t", a[i], $i
            print ""
    } ' file 

这是一个awk一行:

$ awk 'NR==1{gsub("\t","\tspace\t");print;next}!(NR%2){split($0,a,"\t")}NR%2{for (i=1;i<=NF;i++) printf "%s\t%s\t", a[i], $i;print ""} ' file 
200.03  space   468.865 space   567.976 space   647.711
59.927  1576.77 58.099  2192.02 60.546  1630.22 61.461  1879.04 
59.769  1111.36 59.300  2674.2  60.380  1590.89 61.308  970.134 
或以更可读的格式:

$ awk '
    NR==1{
        gsub("\t","\tspace\t")
        print
        next
    }
    !(NR%2){
        split($0,a,"\t")
    }
    NR%2{
        for (i=1;i<=NF;i++) 
            printf "%s\t%s\t", a[i], $i
            print ""
    } ' file 

这需要一个脚本:

$ cat preprocess.awk 
BEGIN {                       # Before processing the file set OFS
    OFS="\t"                  # Set the Output Field Separator to a TAB
}
NR == 1 {                     # Process the header
    for (i=1;i<=NF;i++)       # Loop over every field in the header
        $i=$i OFS             # Add a second TAB to space headers correctly
    print $0                  # Print the header
    next                      # Get the next line in the file
}
NR % 2 {                      # For all the odd lines in the file (y lines)
    line = sep = ""           # Clear the line and separator variables
    for (i=1;i<=NF;i++) {     # Loop over the y value
        line = line sep x[i] OFS $i  # Concatenate x and y values
        sep = OFS             # Set after to avoid leading TAB
    }
    print line                # Print the line
    next                      # Get the next line in the file
}
{                             # If here then we are looking at the even lines (x)
    for (i=1;i<=NF;i++)       # Loop over the x values
        x[i] = $i             # Store values in array x
}
产生:

$ awk -f preprocess.awk file 
h1      h2      h3      h4  
x1  y1  x2  y2  x3  y3  x4  y4
x5  y5  x6  y6  x7  y7  x8  y8
x9  y9  x10 y10 x11 y11 x12 y12

这需要一个脚本:

$ cat preprocess.awk 
BEGIN {                       # Before processing the file set OFS
    OFS="\t"                  # Set the Output Field Separator to a TAB
}
NR == 1 {                     # Process the header
    for (i=1;i<=NF;i++)       # Loop over every field in the header
        $i=$i OFS             # Add a second TAB to space headers correctly
    print $0                  # Print the header
    next                      # Get the next line in the file
}
NR % 2 {                      # For all the odd lines in the file (y lines)
    line = sep = ""           # Clear the line and separator variables
    for (i=1;i<=NF;i++) {     # Loop over the y value
        line = line sep x[i] OFS $i  # Concatenate x and y values
        sep = OFS             # Set after to avoid leading TAB
    }
    print line                # Print the line
    next                      # Get the next line in the file
}
{                             # If here then we are looking at the even lines (x)
    for (i=1;i<=NF;i++)       # Loop over the x values
        x[i] = $i             # Store values in array x
}
产生:

$ awk -f preprocess.awk file 
h1      h2      h3      h4  
x1  y1  x2  y2  x3  y3  x4  y4
x5  y5  x6  y6  x7  y7  x8  y8
x9  y9  x10 y10 x11 y11 x12 y12

如果标头在整个文件中重复,则这可能会起作用:

#!/usr/bin/awk -f

{
    m = NR % 5
    for (i = 1; i <= 4; ++i) {
        a[m, i] = $i
    }
}
m == 0 {
    printf("%8s %8s %8s %8s %8s %8s %8s %8s\n", "" a[1, 1], "space", a[1, 2], "space", a[1, 3], "space", a[1, 4], "space")
    printf("%8s %8s %8s %8s %8s %8s %8s %8s\n", a[2, 1], a[3, 1], a[2, 2], a[3, 2], a[2, 3], a[3, 3], a[2, 4], a[3, 4])
    printf("%8s %8s %8s %8s %8s %8s %8s %8s\n", a[4, 1], a[0, 1], a[4, 2], a[0, 2], a[4, 3], a[0, 3], a[4, 4], a[0, 4])
}

实际上,我按字面意思放置了空格,但您可以将其替换为。

如果标题在整个文件中重复,那么这可能会起作用:

#!/usr/bin/awk -f

{
    m = NR % 5
    for (i = 1; i <= 4; ++i) {
        a[m, i] = $i
    }
}
m == 0 {
    printf("%8s %8s %8s %8s %8s %8s %8s %8s\n", "" a[1, 1], "space", a[1, 2], "space", a[1, 3], "space", a[1, 4], "space")
    printf("%8s %8s %8s %8s %8s %8s %8s %8s\n", a[2, 1], a[3, 1], a[2, 2], a[3, 2], a[2, 3], a[3, 3], a[2, 4], a[3, 4])
    printf("%8s %8s %8s %8s %8s %8s %8s %8s\n", a[4, 1], a[0, 1], a[4, 2], a[0, 2], a[4, 3], a[0, 3], a[4, 4], a[0, 4])
}

实际上,我按字面意思放置了空格,但您可以将其替换为。

@sudo\u O确实如此,但我实际上认为页眉也会重复自身。@sudo\u O是的,就像我说的,我认为页眉也会重复自身。所以它只能这样工作:。无论如何,已经有了一个可接受的解决方案,所以我不会为了以防万一而修改它作为可能的参考。你应该明确它。@sudo\u O确实如此,但我实际上认为头也会重复自身。@sudo\u O是的,就像我说的,我认为头也会重复自身。所以它只能这样工作:。无论如何,已经有了一个可接受的解决方案,所以我不会修改它作为可能的参考,以防万一。那么你应该明确它。