Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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?
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.
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.
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 }
'
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?
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.