Pivot file content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pivot file content
# 1  
Old 06-12-2012
Pivot file content

Hi All ,
I have a file as below .
Code:
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
Code:
A B C D E F
1 2 3 4 5 6
11 21 31 41 51

Please help ..
Please help

Last edited by Franklin52; 06-12-2012 at 10:34 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 06-12-2012
One way:

Code:
echo "A B C D E F"

cat abc124.txt | while read letter number
do
        eval let \${letter}=${number}
        counter=$(($counter + 1))
        if [ ${counter} -ge 6 ]
        then
                echo "$A $B $C $D $E $F"
                counter=0
        fi
done


./scriptname
A B C D E F
1 2 3 4 5 6
11 21 31 41 51 61

There's probably a solution using arrays.

Please mention what Operating System and version you have and what Shell you are using.
This User Gave Thanks to methyl For This Post:
# 3  
Old 06-12-2012
Something like this...

Code:
awk -F\" '{a[$1]=a[$1]" "$2} END{for(i in a) print i,a[i]}' filename|sort|awk '{for(i=1;i<=NF;i++) t[i]=t[i]"\t"$i;if(NF>max)max=NF} END{for(i=1;i<=max;i++)print t[i]}'

This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 06-12-2012
I added a couple entries to the bottom to test robustness..
Code:
A "99"
F "101"

Code:
[mute@geek ~]$ ./pivot input
A       B       C       D       E       F
1       2       3       4       5       6
11      21      31      41      51      61
99                                      101

it's an awk program.
Code:
#!/usr/bin/awk -f
!($1 in a){a[$1]=c;b[c++]=$1}
{gsub(/(^"|"$)/,"",$2);matrix[$1,d[$1]++]=$2}
END {
  for (i = 0; i < c; i++) {
    printf("%s\t", b[i]);
    if (d[b[i]] > max) max=d[b[i]]
  }
  printf("\n")
  for (i = 0; i < max; i++) {
    for (j = 0; j < c; j++)
      printf("%s\t", matrix[b[j],i])
    printf("\n")
  }
}

# 5  
Old 06-12-2012
try:
Code:
awk -F\" '$1==b{if(h){print h; f=1} print s; s=x} !f{h=h $1} NR==1{b=$1} {s=s $2 OFS} END{print s}' infile


Last edited by Scrutinizer; 06-12-2012 at 12:24 PM..
# 6  
Old 06-12-2012
Quote:
Originally Posted by elixir_sinari
Something like this...

Code:
awk -F\" '{a[$1]=a[$1]" "$2} END{for(i in a) print i,a[i]}' filename|sort|awk '{for(i=1;i<=NF;i++) t[i]=t[i]"\t"$i;if(NF>max)max=NF} END{for(i=1;i<=max;i++)print t[i]}'

This is working as per the requirement , but is there a way to format the output .

For eg as of now im getting the output as below.
Code:
     Name     Prompt     Default     ParamType     ParamLength
     "AAAAAAAAA"     "BBBBBBBB"     "CCCCCCCCCCC"     "0"     "0"

Is it possible to give ample spacing between the words ..

Last edited by Scrutinizer; 06-12-2012 at 02:52 PM.. Reason: code tags - erased by OP - and... added again!
# 7  
Old 06-12-2012
With quotes and TAB-separated:
Code:
awk '$1==b{if(h){print h; f=1} print s; s=x} !f{h=h $1 OFS} NR==1{b=$1} {s=s $2 OFS} END{print s}' OFS='\t' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pivot example

Hi all, I am new to shell scripting so pardon me for the questions I will be asking. I was given a task where I have to pivot my data Example Source SGPAPCTUMACCHEA Expected output SGP APC TUM SGP APC ACC SGP APC HEA Can anybody assist me on this?Please use CODE tags as required... (3 Replies)
Discussion started by: redaela
3 Replies

2. Shell Programming and Scripting

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 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... (2 Replies)
Discussion started by: shanlinux
2 Replies

3. 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

4. Shell Programming and Scripting

How to remove exisiting file content from a file and have to append new file content?

hi all, i had the below script x=`cat input.txt |wc -1` awk 'NR>1 && NR<'$x' ' input.txt > output.txt by using above script i am able to remove the head and tail part from the input file and able to append the output to the output.txt but if i run it for second time the output is... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

5. Shell Programming and Scripting

Sed: replace content from file with the content from file

Hi, I am having trouble while using 'sed' with reading files. Please help. I have 3 files. File A, file B and file C. I want to find content of file B in file A and replace it by content in file C. Thanks a lot!! Here is a sample of my question. e.g. (file A: a.txt; file B: b.txt; file... (3 Replies)
Discussion started by: dirkaulo
3 Replies

6. 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

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

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

9. Shell Programming and Scripting

pivot

I have a sql table with : Acitvity Date Value ABC 7/11 10 DEF 7/11 98 ABC 7/12 23 DEF 7/12 100 SER 7/12 67 GRH 7/13 123 HJY 7/14 12 I... (4 Replies)
Discussion started by: mukhanj
4 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