LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 09: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, 09: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, 10: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 10:15 AM.
 
Old 09-27-2007, 10: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 10:52 AM. Reason: tidy up formatting
 
Old 09-27-2007, 02: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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:22 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
Open Source Consulting | Domain Registration