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 06-26-2015, 11:51 AM   #1
arjun_m
LQ Newbie
 
Registered: Jun 2015
Location: India
Distribution: LinuX Mint
Posts: 8

Rep: Reputation: Disabled
Unhappy Transpose only few columns to rows in csv


I have a csv file in this format:

Quote:
file brands kpi country jan01 feb01
a bud aware CN 0.2 0.3
I want to tanspose it in his format I tried a lot but unable to achieve it. Is there some way i can do using awk.

Quote:
file brands kpi country monyy values
a bud aware CN jan01 0.2
a bud aware CN feb01 0.3

Last edited by arjun_m; 06-26-2015 at 11:52 AM.
 
Old 06-26-2015, 02:50 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
With this InFile ...
Code:
file brands kpi country jan01 feb01
a bud aware CN 0.2 0.3
... this code ...
Code:
 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
... produced this OutFile ...
Code:
file  brands  kpi    country  monyy  values
a     bud     aware  CN       jan01  0.2
a     bud     aware  CN       feb01  0.3
Daniel B. Martin
 
Old 06-28-2015, 01:35 AM   #3
arjun_m
LQ Newbie
 
Registered: Jun 2015
Location: India
Distribution: LinuX Mint
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanks Martin. I have recently started learning awk.Could you please provide pointers on how to become an expert in awk
 
Old 06-28-2015, 11: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
Quote:
Originally Posted by arjun_m View Post
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.

Daniel B. Martin
 
Old 06-28-2015, 12:14 PM   #5
arjun_m
LQ Newbie
 
Registered: Jun 2015
Location: India
Distribution: LinuX Mint
Posts: 8

Original Poster
Rep: Reputation: Disabled
Could you please tell how to process this script on the csv file?
 
Old 06-28-2015, 12:15 PM   #6
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
Continuing on the path of incremental improvement, this is a way to eliminate the paste used in post #2.

With this InFile ...
Code:
file brands kpi country jan01 feb01
a bud aware CN 0.2 0.3
... this code ...
Code:
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
... produced this OutFile ...
Code:
file  brands  kpi    country  monyy  values
a     bud     aware  CN       jan01  0.2
a     bud     aware  CN       feb01  0.3
Daniel B. Martin

Last edited by danielbmartin; 06-28-2015 at 12:16 PM. Reason: Cosmetic improvement
 
Old 06-28-2015, 12:18 PM   #7
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 arjun_m View Post
Could you please tell how to process this script on the csv file?
This question is difficult to answer because you didn't provide a csv file.

Daniel B. Martin
 
Old 06-28-2015, 12:21 PM   #8
arjun_m
LQ Newbie
 
Registered: Jun 2015
Location: India
Distribution: LinuX Mint
Posts: 8

Original Poster
Rep: Reputation: Disabled
I am trying to execute the code in this fashion.

awk -f transpose.sh Book1.csv Book2.csv

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

Seems i am lost and unable to execute the code

Last edited by arjun_m; 06-28-2015 at 12:30 PM.
 
Old 06-28-2015, 12:33 PM   #9
arjun_m
LQ Newbie
 
Registered: Jun 2015
Location: India
Distribution: LinuX Mint
Posts: 8

Original Poster
Rep: Reputation: Disabled
Here is the link to the csv file which I want to work on
https://drive.google.com/file/d/0BwI...ew?usp=sharing
 
Old 06-28-2015, 12:36 PM   #10
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 arjun_m View Post
I am trying to execute the code in this fashion.

awk -f transpose.sh Book1.csv Book2.csv

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
Daniel B. Martin
 
Old 06-28-2015, 01:10 PM   #11
arjun_m
LQ Newbie
 
Registered: Jun 2015
Location: India
Distribution: LinuX Mint
Posts: 8

Original Poster
Rep: Reputation: Disabled
Hi martin,

I am unable to achieve the desired result.

Quote:
OutFile ...
BRANDS KPI COUNTRY Sample monyy values
BUDWEISER (ANY) FB CANADA JAN15 Total
Sample (ANY) FB CANADA FEB15
BUDWEISER (ANY) 3FB CANADA monyy values
BUDWEISER (ANY) ECONSID CANADA Total Total
BUDWEISER (ANY) ECONSID CANADA Sample Sample
BUDWEISER (ANY) BRANDAW CANADA monyy values
BUDWEISER (ANY) P7D CANADA Total Total
BUDWEISER (ANY) P7D CANADA Sample Sample
BUDWEISER (ANY) P4W CANADA monyy values
BUDWEISER (ANY) P3M CANADA Total Total
BUDWEISER (ANY) P3M CANADA Sample Sample
BUDWEISER (ANY) EVER CANADA monyy values
BUD LIGHT (ANY) FB Total CANADA
BUD LIGHT (ANY) FB Sample Total
BUD LIGHT (ANY) 3FB monyy values
BUD LIGHT (ANY) ECONSID CANADA CANADA
BUD LIGHT (ANY) ECONSID Total Total
BUD LIGHT (ANY) BRANDAW monyy values
BUD LIGHT (ANY) P7D CANADA CANADA
BUD LIGHT (ANY) P7D Total Total
BUD LIGHT (ANY) P4W monyy values
BUD LIGHT (ANY) P3M CANADA CANADA
BUD LIGHT (ANY) P3M Total Total
BUD LIGHT (ANY) EVER monyy values
BUD LIGHT (ANY) EVER CANADA CANADA
BUD LIGHT (ANY) EVER Total Total
End Of File

Wanted ...
cat: dbm1730wanted.txt: No such file or directory
End Of File

Normal end of job.
What should be the content of dbm1730wanted.txt ?
 
Old 06-28-2015, 01:18 PM   #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 arjun_m View Post
What should be the content of dbm1730wanted.txt ?
That file is the desired result as shown in your original post.

The Wanted file contains ...
Code:
file brands kpi country monyy values
a bud aware CN jan01 0.2
a bud aware CN feb01 0.3
The awk produces this. The column is used for cosmetic purposes, to make an attractive table.

Daniel B. Martin
 
Old 06-28-2015, 01:22 PM   #13
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 arjun_m View Post
I am unable to achieve the desired result.
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.

Daniel B. Martin
 
Old 06-28-2015, 01:26 PM   #14
arjun_m
LQ Newbie
 
Registered: Jun 2015
Location: India
Distribution: LinuX Mint
Posts: 8

Original Poster
Rep: Reputation: Disabled
The code is producing an outfile.txt but not the wanted.txt file
 
Old 06-28-2015, 01:35 PM   #15
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 arjun_m View Post
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.

Daniel B. Martin
 
  


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 script csv editing? Remove rows from csv file that do not contain certain text ingram87 Linux - Software 9 08-03-2012 12:45 PM
[SOLVED] need to make a shell script, transpose a matrix, and average across rows JJB83 Linux - Newbie 6 02-24-2012 02:39 AM
Map 1 CSV's columns to matching columns in another CSV 2legit2quit Programming 7 10-27-2011 08:53 AM
[SOLVED] Transpose multiple rows into a single column wonjusup Linux - Newbie 13 04-09-2011 06:53 AM
convert columns to rows (tab separated file to csv) doug23 Programming 16 08-16-2009 09:14 PM

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

All times are GMT -5. The time now is 08:31 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
Open Source Consulting | Domain Registration