LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-17-2012, 06:14 AM   #1
kbensch
LQ Newbie
 
Registered: Oct 2004
Posts: 5

Rep: Reputation: 0
Creating columns from every 4 rows


Hi

I have now tried perl, bash, awk and who knows what else. Tried searching for transpose row to col and col to row and my frustration level is just rising now. Please can someone help me?

Inpit file like this:
\\MSFILEXZ02\BM_LRO
70.00 GB (Hard)
49.93 GB (71%)
20.07 GB
\\MSFILEXZ02\BM_PracticalClassics
70.00 GB (Hard)
58.73 GB (84%)
11.27 GB
\\MSFILEXZ02\TotalTV_Archive
60.00 GB (Hard)
55.14 GB (92%)
4.86 GB
\\MSFILEXZ02\TVChoice_Archive
460.00 GB (Hard)
391.50 GB (85%)
68.50 GB

I basically need 4 columns from it.

Any help or a virtual slap on the forehead to jog the memory would be appreciated.
 
Old 05-17-2012, 06:42 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
how should the output look like? (what is that 4 columns)? How did you try it?
 
Old 05-17-2012, 06:46 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Maybe something like this?
Code:
awk '{printf "%s\t",$0}; !(NR % 4){printf "\n"}' file
From your sample it produces
Code:
\\MSFILEXZ02\BM_LRO	70.00 GB (Hard)	49.93 GB (71%)	20.07 GB	
\\MSFILEXZ02\BM_PracticalClassics	70.00 GB (Hard)	58.73 GB (84%)	11.27 GB	
\\MSFILEXZ02\TotalTV_Archive	60.00 GB (Hard)	55.14 GB (92%)	4.86 GB	
\\MSFILEXZ02\TVChoice_Archive	460.00 GB (Hard)	391.50 GB (85%)	68.50 GB
with 4 TAB-separated columns (actually 5 being an extra TAB at the end of each line). Is this what you're looking for?
 
Old 05-17-2012, 06:59 AM   #4
kbensch
LQ Newbie
 
Registered: Oct 2004
Posts: 5

Original Poster
Rep: Reputation: 0
Hi

That is exactly what i was trying to do and from your code I now also know where I messed up. Thank you

Quote:
Originally Posted by colucix View Post
Maybe something like this?
Code:
awk '{printf "%s\t",$0}; !(NR % 4){printf "\n"}' file
From your sample it produces
Code:
\\MSFILEXZ02\BM_LRO	70.00 GB (Hard)	49.93 GB (71%)	20.07 GB	
\\MSFILEXZ02\BM_PracticalClassics	70.00 GB (Hard)	58.73 GB (84%)	11.27 GB	
\\MSFILEXZ02\TotalTV_Archive	60.00 GB (Hard)	55.14 GB (92%)	4.86 GB	
\\MSFILEXZ02\TVChoice_Archive	460.00 GB (Hard)	391.50 GB (85%)	68.50 GB
with 4 TAB-separated columns (actually 5 being an extra TAB at the end of each line). Is this what you're looking for?
 
Old 05-17-2012, 07:01 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
ok, that is a possible solution
Code:
awk ' BEGIN { RS="\\\\\\\\" } gsub("\n", "\t") { print ( "\\\\"$0 ) } ' file
 
Old 05-19-2012, 10:49 AM   #6
rootaccess
Member
 
Registered: Mar 2012
Posts: 311

Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
ok, that is a possible solution
Code:
awk ' BEGIN { RS="\\\\\\\\" } gsub("\n", "\t") { print ( "\\\\"$0 ) } ' file
That is great. I was looking for something like this before, too.
Would there be a way to set the tab somehow ("\t") to get the columns to line up?
Great help, pan64
 
Old 05-19-2012, 11:30 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
The problem is the length of the first column. It cannot be lined up with one tab, you need to adjust length.
Code:
awk ' BEGIN { RS="\\\\\\\\"; sp="                              "; OFS="\t" } { $1 = substr($1 sp, 1, 30); print ( "\\\\"$0 ) } ' file
will do the job.




_____________________________________
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
 
Old 05-19-2012, 11:35 AM   #8
rootaccess
Member
 
Registered: Mar 2012
Posts: 311

Rep: Reputation: Disabled
Beautiful. Now I'm going to devote a large number of hours to see exactly what parameters mean what in that command so I can use it for future reference! Thanks alot!
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] transform rows into columns in bash xeon123 Linux - Newbie 4 07-30-2011 07:19 AM
Formatting rows and columns kdelover Programming 4 08-31-2010 02:56 AM
a2ps - How to get 80 rows by 130 columns packer_fan Linux - Software 1 08-28-2010 11:06 AM
columns & rows Ammad Linux - General 1 08-08-2005 04:02 AM
rows and columns digitalgravy Linux - General 2 03-16-2004 06:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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