2

I have a file in the following format:

# Comment 
# Comment 
# Comment
@ Title
@ Label 
@ Label
1       2
2       5
3       7
&
1       7
2       4
3       9
&

So the first column repeats itself, and the second column changes. They're separated by a "&" in the first column and an empty space in the second column. There might be 1000s of such sections. I'd like to cut the portion of the second column and paste it as a new column, so in this case:

# Comment 
# Comment 
# Comment
@ Title
@ Label 
@ Label
1     2     7
2     5     4
3     7     9

Then I could graph all the columns more easily. I also don't care about the first lines, in case is easier to just remove them or leave them there, doesn't matter much.

2
  • Would a 3rd section of two columns be handled in the same way, i.e. that the second column is added as a 4th new column? – Kusalananda Feb 16 '19 at 20:19
  • Correct. If the output has n sections in 2 columns, I'd like to convert that into n columns. – egil137 Feb 16 '19 at 20:34
3

Run it:

cat << EOF | awk '
$1 == "#" || $1 == "@" { print; next }
$1 != "&" { result[$1] = (result[$1] ? result[$1] : $1) "\t" $2 }
END { for (i in result) print result[i] }
'
# Comment 
# Comment 
# Comment
@ Title
@ Label 
@ Label
1       2
2       5
3       7
&
1       7
2       4
3       9
&
EOF

Or you can use the file filename with input data. In this case run:

awk '
$1 == "#" || $1 == "@" { print; next }
$1 != "&" { result[$1] = (result[$1] ? result[$1] : $1) "\t" $2 }
END { for (i in result) print result[i] }
' filename

In both cases you'll get the result:

# Comment 
# Comment 
# Comment
@ Title
@ Label 
@ Label
1   2   7
2   5   4
3   7   9

It will work with any number of columns.

2
  • 1
    it works, thank you! For some reason however the output is out of order, any way to fix this? It's not a big problem since I can fix it in my spreadsheet, but it would help with my worflow. If it matters the numbers in each column can be very large and not only single digits. – egil137 Feb 18 '19 at 15:13
  • @egil137, The easiest way to sort output is to add | sort -n after awk execution. It will look ' | sort -n in the first example and ' filename | sort -n in the second one. – Ivan Olshansky Feb 18 '19 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.