ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Thanks Martin. I have recently started learning awk. Could you please provide pointers on how to become an expert in awk
I am not an expert. All that I know about awk was self-taught, learned from reading posts in this forum. When I see an interesting post I copy-and-paste to make my own running version and then try to improve it. Improvement might be to reduce the number of lines or make it run faster or make it more readable. Go in whichever direction interests you.
Is this the correct way?
transpose.sh is the script file and Book1 is the i/p file and book2 is the o/p file
1) My code is part of a bash program. I suppose it could be coded as a standalone awk program but that's unfamiliar to me.
2) csv stands for comma separated values file. Ordinarily it takes the form of a text file containing information separated by commas, hence the name. The sample input file you provided has no commas. Therefore it is not a csv file (to my understanding.)
This is my program in its entirety.
Code:
#!/bin/bash Daniel B. Martin Jun15
#
# To execute this program, launch a terminal session and enter:
# bash /home/daniel/Desktop/LQfiles/dbm1430.bin
# This program inspired by ...
# http://www.linuxquestions.org/questions/programming-9/
# transpose-only-few-columns-to-rows-in-csv-4175546512/
# Have:
# file brands kpi country jan01 feb01
# a bud aware CN 0.2 0.3
# Want:
# file brands kpi country monyy values
# a bud aware CN jan01 0.2
# a bud aware CN feb01 0.3
# File identification
Path=${0%%.*}
InFile=$Path"inp.txt"
OutFile=$Path"out.txt"
Wanted=$Path"wanted.txt"
echo
echo; echo "Method #1 of LQ Member danielbmartin."
paste -sd" \n" $InFile \
|awk '{mmmdd[1]=$5; mmmdd[2]=$6; rate[1]=$11; rate[2]=$12;
print $1,$2,$3,$4,"monyy values";
for (j=1;j<=2;j++) print $7,$8,$9,$10,mmmdd[j],rate[j]}' \
|column -t >$OutFile
echo; echo "OutFile ..."; cat $OutFile; echo "End Of File"
#echo; echo "Wanted ..."; cat $Wanted; echo "End Of File"
echo
echo; echo "Method #2 of LQ Member danielbmartin."
awk '{mmmdd[1]=$5; mmmdd[2]=$6;
print $1,$2,$3,$4,"monyy values";
getline; rate[1]=$5; rate[2]=$6;
for (j=1;j<=2;j++) print $1,$2,$3,$4,mmmdd[j],rate[j]}' $InFile \
|column -t >$OutFile
echo; echo "OutFile ..."; cat $OutFile; echo "End Of File"
echo; echo "Wanted ..."; cat $Wanted; echo "End Of File"
echo; echo "Normal end of job.";echo; exit
Thank you for providing the unwanted OutFile. It would be even more helpful for you to provide the real-world InFile. I wrote code to fit your original small sample. Perhaps that code wasn't what you really needed.
The code is producing an outfile.txt but not the wanted.txt file
The "wanted" file was copy-and-pasted from your original post.. I found it useful to do this for debugging. It enabled an easy "eyeball test" to verify that the code was working.
As this forum exchange develops I get the feeling that we are not communicating well. This may be my fault for not understanding your problem. If I have led you down a wrong path, I apologize.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.