LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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, 07: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, 07:42 AM   #2
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,139

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

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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: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, 08:01 AM   #5
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,139

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

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, 12:30 PM   #7
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,139

Rep: Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363
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, 12:35 PM   #8
rootaccess
Member
 
Registered: Mar 2012
Posts: 211

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


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 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 08:19 AM
Formatting rows and columns kdelover Programming 4 08-31-2010 03:56 AM
a2ps - How to get 80 rows by 130 columns packer_fan Linux - Software 1 08-28-2010 12:06 PM
columns & rows Ammad Linux - General 1 08-08-2005 05:02 AM
rows and columns digitalgravy Linux - General 2 03-16-2004 07:47 PM


All times are GMT -5. The time now is 05:15 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration