Select portions of one column and paste as new column












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.










share|improve this question

























  • 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 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 at 20:34
















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.










share|improve this question

























  • 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 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 at 20:34














2












2








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.










share|improve this question
















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.







text-processing awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 17 at 7:22









Rui F Ribeiro

41k1479137




41k1479137










asked Feb 16 at 20:12









egil137egil137

132




132













  • 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 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 at 20:34



















  • 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 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 at 20:34

















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 at 20:19





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 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 at 20:34





Correct. If the output has n sections in 2 columns, I'd like to convert that into n columns.

– egil137
Feb 16 at 20:34










1 Answer
1






active

oldest

votes


















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.






share|improve this answer





















  • 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 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 at 18:42













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501091%2fselect-portions-of-one-column-and-paste-as-new-column%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer





















  • 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 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 at 18:42


















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.






share|improve this answer





















  • 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 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 at 18:42
















3












3








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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 17 at 8:08

























answered Feb 17 at 1:42









Ivan OlshanskyIvan Olshansky

176116




176116








  • 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 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 at 18:42
















  • 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 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 at 18:42










1




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 at 15:13





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 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 at 18:42







@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 at 18:42




















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501091%2fselect-portions-of-one-column-and-paste-as-new-column%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

is 'sed' thread safe

How to make a Squid Proxy server?