LinuxQuestions.org
Visit Jeremy's Blog.
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-02-2014, 02:41 AM   #1
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Rep: Reputation: 16
transpose data in a file


I've a set of information in a file which is in vertical format. I need to transpose the information in horizontal format.

//// information in the file
bankId: 1VVV2564
custCnt: 4598
totalAmt: $42,256,896

bankId: 1WTY0264
custCnt: 1256
totalAmt: $56,896
/// transposing the information

bankId custCnt totalAmt
1VVV2564 4598 $42,256,896
1WTY0264 1256 $56,896
///////////////////////////////////////////////////
How can I do this kind of data transformation using shell script?

Last edited by jone kim; 05-02-2014 at 02:42 AM. Reason: left some information
 
Old 05-02-2014, 10:02 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Below uses a file named "testfile" - you'd substitute the real name of your file for that.

Code:
echo "bankid custCnt totalAmt"
egrep "bankId:|custCnt:|totalAmt:" testfile |awk '/bankId:/ {printf $2" "}
/custCnt:/ {printf $2" "}
/totalAmt:/ {print $2}'
Above is a good example of getting multiple lines from a file with awk
It first locates the patterns then printf outputs from the first pattern line then the next pattern line etc... Note that final is
a "print" rather than a "printf". You could use a printf on final line but would have to then use $2"\n" to include a line feed.
 
1 members found this post helpful.
Old 05-03-2014, 09:49 AM   #3
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Original Poster
Rep: Reputation: 16
Thanks MensaWater.

this is very useful.

Can I ask you another question, which is being quite doubt in my mind.

What if the first column is a variable, I mean the first column values changes with the type of information in the file.
How can we transpose the data in such cases?

Thank You!!!
 
Old 05-03-2014, 11:12 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by MensaWater View Post
Below uses a file named "testfile" - you'd substitute the real name of your file for that.

Code:
echo "bankid custCnt totalAmt"
egrep "bankId:|custCnt:|totalAmt:" testfile |awk '/bankId:/ {printf $2" "}
/custCnt:/ {printf $2" "}
/totalAmt:/ {print $2}'
why do you need egrep at all? awk '...' testfile should be ok. Also there is a problem with this code, it will print the values in the order of occurrence, will not reorder them and will do interesting things if something were missing, mixed, duped (corrupted) in the source file.
 
Old 05-05-2014, 09:12 AM   #5
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
What I wrote does was the OP requested. Yes doing a "cat testfile | awk" or doing the awk " " testfile work as well.

It was a quick and dirty to do what the OP asked. If you have better suggestions how to handle all forms of data why not post them rather than punching holes in what I wrote? Nothing in my post said it handle all error checking for all forms of data - it does however handle the data he provided.
 
Old 05-06-2014, 12:32 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
are you kidding? in my practice not only the solution is important but the usability too: how it will work with other data, how it will handle corrupt input - what about error handling .... This is not punching holes but knowing the limits. You gave us a working solution, I tried to improve it a bit and also found a possible problem. We have not seen all the input lines (so yes, it will surely handle the data provided, but what about the other lines?)

Yes, I have another idea, that will be the answer to #3, it is a more general solution. Unfortunately I have had not enough time to present it, so this is now only a simple pseudo-code, not tested at all:
awk ' { a[$1] = $2 } # will store all the var=value pairs in an array
{ some condition if the data is collected, can be printed:
for x in a print a[x] # so print the elements of that array
clear the array and start over }
'
 
Old 05-06-2014, 06:27 PM   #7
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Original Poster
Rep: Reputation: 16
To pan64:

Thanks for the suggestion.

Please can you give us the implemented code as directed by your suggestions. That will be more beneficial to us.

Thanks!
 
Old 05-06-2014, 06:33 PM   #8
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Original Poster
Rep: Reputation: 16
@ MensaWater:

You wrote that:

Quote:
It was a quick and dirty to do what the OP asked.
can we optimize your solution?

and what about my another concern;

Quote:
What if the first column is a variable, I mean the first column values changes with the type of information in the file.
How can we transpose the data in such cases?
Thank You!!!
 
Old 05-07-2014, 07:56 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Code:
awk ' length { a[$1] = $2; next } 
      { for ( t in a ) printf t "\t"
        printf "\n"
        for ( t in a ) printf a[t] "\t"
        delete a
        printf "\n"
      }' inputfile
this can also be improved
 
Old 05-07-2014, 08:13 AM   #10
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by pan64 View Post
Code:
awk ' length { a[$1] = $2; next } 
      { for ( t in a ) printf t "\t"
        printf "\n"
        for ( t in a ) printf a[t] "\t"
        delete a
        printf "\n"
      }' inputfile
this can also be improved
Running that only provides the header and the first record:
custCnt: bankId: totalAmt:
4598 1VVV2564 $42,256,896
 
Old 05-07-2014, 08:16 AM   #11
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by jone kim View Post
can we optimize your solution?
Thank You!!!
The idea in this forum is to help others learn not to write your code for you. You are certainly welcome to optimize but really ought to be researching further yourself. My post was to show you how to accomplish your basic task and explain what I did in doing so. You might wish to check out "awk tutorial" in a web search. Once you come up with ideas based on that and try them then you should ask questions at point where you get stuck.
 
Old 05-07-2014, 08:36 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by MensaWater View Post
Running that only provides the header and the first record:
custCnt: bankId: totalAmt:
4598 1VVV2564 $42,256,896
it requires an empty line as delimiter between records. You need to insert an additional line at the end of the input file.
I agree with you (#11), therefore it is only an idea not a complete solution.
 
Old 05-11-2014, 04:26 AM   #13
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Original Poster
Rep: Reputation: 16
Thank you for the replies guys.

I did not have clear idea to solve the problem. I did not know where to start with, so I needed help with you guys who have prior knowledge.
Now I can research on this and may be if I come up with new ideas to solve this I'll definitely post it.

Thanks guys for the comments.

Thank You very much!!!
 
  


Reply

Tags
shell script



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] Help me in transpose the file column to row freshlinux Linux - Newbie 2 09-06-2013 03:40 AM
Want to transpose the text file content sekaranarun Linux - Newbie 1 09-03-2013 06:18 AM
[SOLVED] Transpose a column to a custom row t.othoneos Linux - Newbie 7 03-15-2012 03:06 PM
[SOLVED] AWK - transpose czezz Programming 4 12-28-2010 08:36 AM
transpose row to column? resolute155 Programming 3 09-07-2009 02:29 PM

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

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