LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to split single column string into two columns (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-split-single-column-string-into-two-columns-907013/)

Colorinb 10-07-2011 06:40 PM

How to split single column string into two columns
 
Hi, I'm new to Linux, after I grep "Size" and "Price" from another file, I have a single column string with following format:

Size: S
Size: M
Size: L
Size: XL
Price: 1
Price: 2
Price: 3
Price: 4
...

I want to split it into two columns with the following format

Size: S Price: 1
Size: M Price: 2
Size: L Price: 3
Size: XL Price: 4

May I know how to accomplish this, is sed a possible solution? thanks!

crts 10-07-2011 06:53 PM

Hi,

here is an awk solution:
Code:

awk '/Size/ {a[i++]=$0};/Price/ {print a[NR-i-1]" "$0}' file
Assuming that there are as many Size fields as Price fields.

Colorinb 10-07-2011 09:06 PM

Quote:

Originally Posted by crts (Post 4492749)
Hi,

here is an awk solution:
Code:

awk '/Size/ {a[i++]=$0};/Price/ {print a[NR-i-1]" "$0}' file
Assuming that there are as many Size fields as Price fields.

Thanks a lot! It's work!


All times are GMT -5. The time now is 03:42 AM.