LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-21-2013, 08:44 PM   #1
Guyverix
Member
 
Registered: Nov 2008
Location: Washington State
Distribution: Mint
Posts: 36

Rep: Reputation: 2
bash suggestions to convert horizontal columns to vertical columns


I am attempting what I ~thought~ was kind of straight forward. I am pulling column data from a Windows host via bash, and the results are good but the format is not what I am trying for. Here is what I get:

AvgDiskBytesPerRead|AvgDiskBytesPerTransfer|AvgDiskBytesPerWrite|AvgDiskQueueLength|AvgDiskReadQueue Length|AvgDisksecPerRead|AvgDisksecPerTransfer|AvgDisksecPerWrite|AvgDiskWriteQueueLength|(null)|Cur rentDiskQueueLength|Description|DiskBytesPersec|DiskReadBytesPersec|DiskReadsPersec|DiskTransfersPer sec|DiskWriteBytesPersec|DiskWritesPersec|FreeMegabytes|Frequency_Object|Frequency_PerfTime|Frequenc y_Sys100NS|Name|PercentDiskReadTime|PercentDiskTime|PercentDiskWriteTime|PercentFreeSpace|PercentIdl eTime|SplitIOPerSec|Timestamp_Object|Timestamp_PerfTime|Timestamp_Sys100NS
0|0|0|0|0|0|0|0|0|(null)|0|(null)|0|0|0|0|0|0|18196|0|0|0|C:|0|0|0|44|100|0|0|0|0
0|0|0|0|0|0|0|0|0|(null)|0|(null)|0|0|0|0|0|0|132411|0|0|0|D:|0|0|0|80|100|0|0|0|0
0|0|0|0|0|0|0|0|0|(null)|0|(null)|0|0|0|0|0|0|150607|0|0|0|_Total|0|0|0|73|100|0|0|0|0


What I am attempting is this:

AvgDiskBytesPerRead 0 0 0
AvgDiskBytesPerTransfer 0 0 0
... etc ...

As you can tell from the output, at minimum it will have two lines of return, and max will be variable based on hard drives.

I have messed with using tr and awk to break things up in a for loop, and I can make each line display vertically, however when adding the additional column output I just cannot make it work.

Any suggestions or direction would be greatly appreciated.
 
Old 01-21-2013, 10:25 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
This is not elegant but it works...
Code:
# Initialize the Output file
sed q $InFile |sed 's/|/\n/g' > $OutFile

# Lines = number of lines in the input file
Lines=$(cat $InFile |wc -l)  

for (( c=2; c<=$Lines; c++ ))
do
   sed -n $c'p' $InFile \
  |sed 's/|/\n/g'       \
  |paste $OutFile - > $Work1
  cat $Work1 > $OutFile
done
Daniel B. Martin

Last edited by danielbmartin; 01-21-2013 at 10:49 PM. Reason: Tighten the code, slightly
 
1 members found this post helpful.
Old 01-22-2013, 12:35 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
awk is ok, can you show us what have you tried?

Theoretically you would need two loops: the first one to read all the lines into arrays the second is to display the result
 
Old 01-22-2013, 07:48 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
This awk solution is more concise than the one given in post #2 of this thread and does not use a temporary work file.
Code:
awk -F "|" '{for (i=1; i<=NF; i++) arr[i]=arr[i]$i"\t"} 
  END {i=1; while (i in arr) {print arr[i]; i++;}}'  \
  $InFile > $OutFile
Daniel B. Martin
 
1 members found this post helpful.
Old 01-22-2013, 10:07 AM   #5
Guyverix
Member
 
Registered: Nov 2008
Location: Washington State
Distribution: Mint
Posts: 36

Original Poster
Rep: Reputation: 2
Thank you very much! I went with answer #3, as I was able to run it inline with the WQL query itself and datafill a base varaible. With this, I can really make something useful here! Thank you again!
 
Old 01-22-2013, 10:42 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
glad to help you
Just in case you really want to say thanks press YES (bottom right corner)
 
Old 01-22-2013, 08:31 PM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Using same concept you could do that in Bash as well:
Code:
#!/bin/bash

while IFS=\| read -a LINE; do
    for I in "${!LINE[@]}"; do
        if [[ -n ${ARR[I]} ]]; then
            ARR[I]=${ARR[I]}$'\t'${LINE[I]}
        else
            ARR[I]=${LINE[I]}
        fi
    done
done

IFS=$'\n' A="${ARR[*]}"
echo "$A"
Code:
bash script.sh < INPUT > OUTPUT
 
Old 01-23-2013, 02:00 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Just for another fun alternative:
Code:
ruby -ne 'BEGIN{a=[]};a << $_.chomp.split("|");END{a.transpose.each{|x| puts x.join(" ")}}' file
 
1 members found this post helpful.
Old 01-23-2013, 07:19 AM   #9
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
Just for another fun alternative ...
I tested your ruby solution and got this error:
Code:
-e:1: undefined local variable or method `a' for main:Object (NameError)
Please advise.

Daniel B. Martin
 
Old 01-23-2013, 09:24 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Hmmm ... initially I would have said you missed the BEGIN ... which version of ruby are you running? Unfortunately there are a few quirks which catch me out between versions
Code:
grail@pilgrim:~$ ruby -v
ruby 1.9.3p362 (2012-12-25 revision 38607) [x86_64-linux]
 
Old 01-23-2013, 09:32 AM   #11
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
some of the above solutions are probably more elegant but heres an idea:

separate each row into a file with a single column:
Code:
sed -n 1p guy.txt | tr '\|' '\n' > guy-1.txt
then paste all the separated columns together:
Code:
paste guy-*.txt > guy-columns.txt
i am too lazy to wrap this up in a for loop at the moment.
 
Old 01-23-2013, 09:39 AM   #12
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by schneidz View Post
i am too lazy to wrap this up in a for loop at the moment.
Take a look at post #2 in this thread. It uses the same idea, wrapped up in a loop.

Daniel B. Martin
 
Old 01-23-2013, 07:41 PM   #13
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
grail's solution worked for me.
Code:
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
 
Old 01-23-2013, 07:54 PM   #14
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by konsolebox View Post
grail's solution worked for me.
Code:
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
I've got
Code:
ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
Perhaps that explains the error message cited previously.

Daniel B. Martin
 
Old 01-24-2013, 11:03 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
I believe the below excerpt 'may' identify the problem:
Quote:
Ruby 1.9 introduces many significant changes over the 1.8 series.[21] Examples:
Block local variables (variables that are local to the block in which they are declared)
Unfortunately I started with 1.9 so am unsure on what is required to be changed to make it work in the older version
 
  


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
[SOLVED] How to convert the rows of data into the columns? w1k0 Programming 101 02-06-2014 09:24 PM
SQL statements howto -- 3 columns input but 2 columns output fhleung Programming 3 11-29-2012 10:45 AM
[SOLVED] AWK: add columns while keep format for other columns cristalp Programming 3 10-13-2011 06:14 AM
Convert columns of data into rows jaufer Linux - Software 3 06-29-2010 05:19 PM
Script to convert logs columns to rows fono Linux - Software 10 05-19-2009 08:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:11 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