Split columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split columns
# 1  
Old 01-04-2011
Split columns

Greetings,

I have an input file with two columns. The first column has different codes. Each time I have a new code I need to split the columns into new ones. The input file looks like this
Code:
CR124	1320
CR124	1138
CR124	682
CR124	629
CR124	592
CR124	580
CR124	537
CR161	3967
CR161	3656
CR161	3235
CR161	3021
CR199	2741
CR199	2565

So I would need to get this
Code:
CR124	1320	CR161	3967	CR199	2741
CR124	1138	CR161	3656	CR199	2565
CR124	682	CR161	3235	 	 
CR124	629	CR161	3021	 	 
CR124	592	 	 	 	 
CR124	580

Can someone help me to achieve this. Thanks in advance!


Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples!

Last edited by Franklin52; 01-04-2011 at 03:42 AM..
# 2  
Old 01-04-2011
Create an array that contains the final printing lines. Each time the value in the first column is not equal to the one in the preceding line reset the array index to 1 . For every line append the content of every line to the end of the Array value with the current index number and raise the index number after that. Record the maximum index. When the file is read print the contents of the array.
# 3  
Old 01-04-2011
Code:
open (FIN, "temp.txt"); # your input file name
read FIN, $file, -s FIN;
close FIN;
@key=$file=~m{CR\d+}g;
@ukey = grep { ! $exist { $_ }++ } @key;
$max=0;
foreach (@ukey)
{
 $cnt=0;
 while ( $file =~ m{$_\s+(\d+)}g)
 {
  $hash{$_}->[$cnt]=$1;
  $cnt++;
 }
 $max = ($max<$cnt)?$cnt:$max;
}
for ($i=0; $i<$max; $i++)
{
 for ($j=0; $j<=$#ukey; $j++)
 {
  print $ukey[$j],"\t",$hash{$ukey[$j]}->[$i],"\t" if($hash{$ukey[$j]}->[$i]);
 }
 print "\n";
}

Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples.

Last edited by Franklin52; 01-04-2011 at 03:42 AM..
# 4  
Old 01-04-2011
Code:
awk 'p!=$1{i=1;p=$1} {A[i++]=A[i]$0 FS} i>m{m=i} END{for(i=1;i<=m-1;i++)print A[i]}' infile

# 5  
Old 01-04-2011
Great,

Hi Scrutinizer,

I am very new to shell scripting. Could you please suggest some shell scripting fundamental tutorials for me. It's helpful me at lot.

Thanks,
Mani Muthu
# 6  
Old 01-04-2011
Quote:
Originally Posted by Scrutinizer
Code:
awk 'p!=$1{i=1;p=$1} {A[i++]=A[i]$0 FS} i>m{m=i} END{for(i=1;i<=m-1;i++)print A[i]}' infile

Hello Scrutinizer,

I would like to find out how this awk array works. I only need to learn what initial values the code will have, then i can complete the rest of the process, so could you correct me if im wrong:

P=0, i=0, m=0
0 != "CR124" {i=1,P="CR124"}
{A[2]=A[1]"CR124 1320" FS} ## Here what is the value of array A[1] ??##
2>0{m=2}
END {for(i=1,1<=1;i=2) print A[2]}

Or could you give the result in a matrix form, if would appreciate it..

Thanks in advance

Last edited by EAGL€; 01-04-2011 at 06:00 AM..
# 7  
Old 01-04-2011
Code:
p="", i=0, m=0
"" != "CR124" {i=1,p="CR124"}
A[1]=A[1]"CR124 1320" FS} # result first time: A[1]="CR124 1320 "
2>0{m=2}
END {for(i=1,1<=1;i=2) print A[1]}

---------- Post updated at 14:49 ---------- Previous update was at 11:00 ----------

Hi Mani Muthu,

That wasn't shell scripting but an awk script. An equivalent shell script would be look something like this if done in ksh:
Code:
#!/bin/ksh
while read -A L
do
  if [[ $p != ${L[0]} ]]; then
    p=${L[0]}
    i=0
  fi
  A[$((i++))]+="${L[@]} "
  if (( i>m )); then
    m=$i
  fi
done < infile
for ((i=0;i<=m-1;i++))
do
  printf "%s\n" "${A[i]}"
done

awk is powerful and compact language. I used O'Reilly's sed & awk to learn awk (and some practice).
These 2 Users Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using awk to split a column into two columns

Hi, I am trying to split the following output into two columns, where each column has Source: Destination: OUTPUT TO FILTER $ tshark -r Capture_without_mtr.pcap -V | awk '/ (Source|Destination): /' | more Source: x.x.x.x Destination: x.x.x.x Source:... (2 Replies)
Discussion started by: sand1234
2 Replies

2. Shell Programming and Scripting

Split columns into rows

Any one can help me in converting columns into rows. example I have input file 10000| 10002| 10003| 10004| 10005| I want output in below format PARTY|PART_DT 10000|12080000000 10002|13075200000 10003|13939200000 10004|1347200000 10004|133600000 10004|1152000000 (13 Replies)
Discussion started by: syd
13 Replies

3. Shell Programming and Scripting

Split multi columns line to 2 columns

I have data like this 1 a,b,c 2 a,c 3 b,d 4 e,f would like convert like this 1 a 1 b 1 c 2 a 2 c 3 b 3 d 4 e 4 f Please help me out (4 Replies)
Discussion started by: jhonnyrip
4 Replies

4. Shell Programming and Scripting

Split 5 columns into 3 columns

I have a big data set as follows 8.519 8.601 8.833 9.183 9.602 1.003 1.041 1.070 1.085 1.084 1.06 1.034 9.879 9.307 8.66 I simply want this to arrange like this 8.519 8.601 8.833 9.183 9.602 1.003 1.041 1.070 1.085 1.084 1.06 1.034 9.879 9.307 8.66 ... (2 Replies)
Discussion started by: cnn
2 Replies

5. Shell Programming and Scripting

How to split all columns into multiple columns?

Hi, all. How can I split all columns into multiple columns separated by tab? Input: qq ee TT 12 m1 aa gg GG 34 2u zz dd hh 56 4h ww cc JJ 78 5y ss ff kk 90 j8 xx pp mm 13 p0 Output: q q e e T T 1 2 m 1 a a g g G G 3 4 2 u z z d d h h 5 6 4 h w w c c J J 7 8 5 y (8 Replies)
Discussion started by: huiyee1
8 Replies

6. Programming

Split the columns in Perl scripting

hi all, i have a file like thsi a 1 3;4 b 2 4;7 c 4 5;6 d 4 5;8 now i want 1st, 2nd and in 3rd column i want only 1st column the output should be like below a 1 3 b 2 4 c 4 5 d 4 5 I need it in perl; please dont write in Shell scripting (1 Reply)
Discussion started by: siva kumar
1 Replies

7. Shell Programming and Scripting

To split the columns

Hi, I have a file /tmp/1.txt. (Space b/w the fields are not equal) #cat 1.txt 35G 22G 13G 64% /mount1 15G 7.1G 7.5G 49% /mount2 2G 9.9G 4.7G 68% /mount3 15G 9.1G 5G 63% /mount4 i want to wite a script to display these values as below. O/P: 35G-22G-13G-64%-/mount1... (15 Replies)
Discussion started by: thomasraj87
15 Replies

8. UNIX for Dummies Questions & Answers

Split columns

Greetings, I have an input file with two columns. The first column has different codes. Each time I have a new code I need to split the columns into new ones. The input file looks like this CR124 1320 CR124 1138 CR124 682 CR124 629 CR124 592 CR124 580 CR124 537 CR161 3967... (2 Replies)
Discussion started by: vanesa1230
2 Replies

9. Shell Programming and Scripting

split one column into multiple columns

hey, i have the following data: 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 (7 Replies)
Discussion started by: zaneded
7 Replies

10. Web Development

split the fields in a column into 3 columns

Have a column "address" which is combination of city, region and postal code like. Format is : city<comma><space>region<space>postal code abc, xyz 123456 All these three city, region and postal code are not mandatory. There can be any one of the above. In that case a nell... (2 Replies)
Discussion started by: rakshit
2 Replies
Login or Register to Ask a Question