Pivot file using shellscript


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pivot file using shellscript
# 1  
Old 04-14-2017
Pivot file using shellscript

Hi, I am new at using shell scripting. So I have a question for the more experienced Linux users. I would like to perform some kind of pivot on a file.

Example Input file
Code:
COL1,"2001-01-01","2001-03-01","2001-03-24"
A1,22,,44
B1,56,78,12
C2,5,,

I would like to have to following output
Code:
A1,"2001-01-01",22
A1,"2001-03-24",44
B1,"2001-01-01",56
B1,"2001-03-01",78
B1,"2001-03-24",12
C2,"2001-01-01",5

Any help is appreciated.

Last edited by vgersh99; 04-14-2017 at 09:56 AM.. Reason: code tags, please!
# 2  
Old 04-14-2017
Code:
awk '
  FNR==1 { n=split($0, a,FS); next}
  {
     for(i=2;i<=NF;i++)
        if ($i)
            printf("%s%s%s%s%s\n", $1, OFS, a[i], OFS,$i)
  }' FS=, OFS=, myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 04-14-2017
Awesome vgersh99. That is precisely what I was looking for. Smilie
Thank you very much
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create a pivot table from CSV file

Gents, Can you please help me to create a pivot table from a csv file. ( I have zip the csv file) Using the file attached, columns 1,28 and 21 i would like to get something like this output JD Val 1 2 3 4 5 6 7 8 9 10 11 12 Total... (4 Replies)
Discussion started by: jiam912
4 Replies

2. Shell Programming and Scripting

How to pass a shellscript variable to a sql file?

Hi, i wan't to pass a shellscript variable to a sql file. a.sql select $field from dual; the way i am calling this is through sqlplus field_name="sysdate" sqlplus -s username/password@hostname:port/servicename <<EOF @a.sql $field_name EOF (4 Replies)
Discussion started by: reignangel2003
4 Replies

3. Shell Programming and Scripting

Pivot file content

Hi All , I have a file as below . A "1" B "2" C "3" D "4" E "5" F "6" A "11" B "21" C "31" D "41" E "51" F "61" And the output should be like A B C D E F 1 2 3 4 5 6 11 21 31 41 51 (8 Replies)
Discussion started by: saj
8 Replies

4. UNIX for Dummies Questions & Answers

Script to pivot flat file

Hi to all, I have a file with a list of values: v1,v2,v3....,v9 need file in this format: Name1: v1 Name2: v2 Name3: v3 ... Name9: v9 Please help me out. (2 Replies)
Discussion started by: mozi
2 Replies

5. Shell Programming and Scripting

Please help me to print particular line in the file using shellscript

Plese help me on this below I have to do below 2 things in a single shell script 1) I have to search one word(eg : red) from the below sample file. 2) Then if I am getting word in the 3rd line (red) then I need to print the above 2 lines and below 2 lines from thekeyword For Example:... (5 Replies)
Discussion started by: Maanjesh
5 Replies

6. Shell Programming and Scripting

How to rename file-using shellscript?

Hi All, need your help in renaming the file ? Please read the below data carefully. Its may b simple for you :-) just information is too long Eg-home/user01/exercise contains abc.log, ppp.log ,shd.log want to rename this file with timestamp using shellscript for this i... (8 Replies)
Discussion started by: aish11
8 Replies

7. Shell Programming and Scripting

Pivot file contents

Hi All, I am trying to pivot the contents in a file. Ex: I have a file sample.txt with data "A B C D", i need the contents to pivot & resulting file should look like "A B C ... (3 Replies)
Discussion started by: new_ds_man
3 Replies

8. Shell Programming and Scripting

Check file exists from a shellscript

Hi, I have a list of files that I want to check to see if they exist and then count how many of these files exist, I also want to do the same for the files that arent found. I have done this by creating temp files see below but want ot do this using variables instead: for FILE in... (7 Replies)
Discussion started by: tonydsam
7 Replies

9. Shell Programming and Scripting

Pivot variable record length file and change delimiter

Hi experts. I got a file (500mb max) and need to pivot it (loading into ORCL) and change BLANK delimiter to PIPE |. Sometimes there are multipel BLANKS (as a particular value may be BLANK, or simply two BLANKS instead of one BLANK). thanks for your input! Cheers, Layout... (3 Replies)
Discussion started by: thomasr
3 Replies

10. Shell Programming and Scripting

Converting Pivot file to flat file

I have a file in this format. P1 P2 P3......................... A001 v11 v21 v31...................... A002 v12 v22 v32............................ A003 v13 v23 v33.......................... A004 v14 v24 v34.............................. . . . A00n... (2 Replies)
Discussion started by: vskr72
2 Replies
Login or Register to Ask a Question