LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 09-27-2007, 10:11 AM   #1
btm
LQ Newbie
 
Registered: Mar 2007
Posts: 4

Rep: Reputation: 0
Moving columns with sed or awk?


I have a text file that I have parsed with commas using sed. There are also spaces between the columns (making them fixed width) which I strip out in the end to make it a true csv file for exportation to a database.

Question:
I need to rearrange some of the columns. I.E. column 4 needs to be after column 1, etc.
 
Old 09-27-2007, 10:41 AM   #2
ChrisScott
Member
 
Registered: Nov 2006
Location: St Albans, England
Distribution: Fedora c3/5, Suse pro 10/openSuse 10.2, RHES, Zenwalk.....
Posts: 97

Rep: Reputation: 15
You could use SED for that (lots of alternatives as well, I expect!):
Code:
sed 's@\(.*\),\(.*\),\(.*\)@\2,\1,\3@g' file
ie,

1,4,7
2,3,4

becomes,

4,1,7
3,2,4
 
Old 09-27-2007, 11:14 AM   #3
ChrisScott
Member
 
Registered: Nov 2006
Location: St Albans, England
Distribution: Fedora c3/5, Suse pro 10/openSuse 10.2, RHES, Zenwalk.....
Posts: 97

Rep: Reputation: 15
You could use a sed script to reajust your columns to:
Code:
cat <<EOF>sedscript
s@\(.*\),\(.*\),\(.*\)@,\2,\1,\3@g
s@ *,@,@g
: width
s@,\([^,]\{0,10\}\),@,\1 ,@g
t width
s@^,@@g
EOF
sed -f sedscript file
the 10 in - s@,\([^,]\{0,10\}\),@,\1 ,@g - is the width the column will become.

Last edited by ChrisScott; 09-27-2007 at 11:15 AM.
 
Old 09-27-2007, 11:50 AM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
You can use awk:
Code:
awk 'BEGIN {OFS = ","}; {print $1,$4,$2,$3}' your.file > your_output.file
The {OFS = ","} sets the output field separator as a comma, allowing for your csv file creation. The $1,$4 bit reorders to columns according to their numerical value.

Last edited by pwc101; 09-27-2007 at 11:52 AM. Reason: tidy up formatting
 
Old 09-27-2007, 03:03 PM   #5
btm
LQ Newbie
 
Registered: Mar 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks, all. ALL of your solutions work perfectly for me.
 
  


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
Removing columns from a file using `sed` indiancosmonaut Programming 6 10-04-2007 01:42 AM
awk command to merge columns from two separate files into single file? johnpaulodonnell Linux - Newbie 4 01-23-2007 11:10 AM
Sed and Awk Gins Programming 7 04-19-2006 11:32 AM
sed script to read only columns 4 to 6 in output database cranium2004 Programming 10 02-28-2006 08:20 AM
awk/sed help pantera Programming 1 05-14-2004 12:59 AM


All times are GMT -5. The time now is 01:46 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration