LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-07-2011, 07:40 PM   #1
Colorinb
LQ Newbie
 
Registered: Oct 2010
Posts: 8

Rep: Reputation: 0
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!
 
Old 10-07-2011, 07:53 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
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.

Last edited by crts; 10-07-2011 at 07:54 PM.
 
Old 10-07-2011, 10:06 PM   #3
Colorinb
LQ Newbie
 
Registered: Oct 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Insert column with awk or sed between two columns captainentropy Linux - Newbie 9 11-27-2014 12:49 PM
sort multiple columns + replace another column cedance Linux - Newbie 1 03-29-2011 06:59 AM
How to get parts of one column and prints all columns ? nasra2002 Linux - Software 2 12-13-2010 10:36 PM
awk multiple column into single column ilukacevic Programming 49 07-19-2010 08:23 PM
[perl]How to treat string like "a b" as a single string when split? john.daker Programming 21 06-01-2009 06:57 PM


All times are GMT -5. The time now is 04:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration