column to rows based on another column...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting column to rows based on another column...
# 1  
Old 07-06-2009
column to rows based on another column...

Guys,
i have a file in below format where the barcode's are uniq per site but could be repeated for different site. so i want to convert the site column to rows based on the barcode's as below output.
your help is appreciated!!!

input:
SITE BARCODE QTY SP CP
10001 6281103890017 10 50 48
10001 6281103890031 2 12 10
10001 6281103890024 5 25 22
10002 6281103890017 13 50 48
10002 6281103890031 1 12 10
10002 6281103890024 2 25 22
10003 6281103890017 20 50 48
10003 6281103890031 4 12 10
10003 6281103890024 10 25 22
output:
10001 10002 10003
BARCODE QTY SP CP QTY SP CP QTY SP CP
6281103890017 10 50 48 13 50 48 20 50 48
6281103890031 2 12 10 1 12 10 4 12 10
6281103890024 5 25 22 2 25 22 10 25 22

Last edited by malcomex999; 07-06-2009 at 10:50 PM..
# 2  
Old 07-07-2009
Code:
while(<DATA>){
	chomp;
	my @tmp=split;
	if($.==1){
		$header=sprintf("%s %s %s %s %s %s %s %s %s %s ",$tmp[1],$tmp[2],$tmp[3],$tmp[4],$tmp[2],$tmp[3],$tmp[4],$tmp[2],$tmp[3],$tmp[4]);
		next;
	}
	$site{$tmp[0]}=$.;
	$hash{$tmp[1]}.=$tmp[2]." ".$tmp[3]." ".$tmp[4]." ";
	$seq{$tmp[1]}=$.;	
}
print join " ", sort {$site{$a}<=>$site{$b}} keys %site;
print "\n$header\n";
for my $id (sort {$seq{$a}<=>$seq{$b}} keys %hash){
	print $id," ",$hash{$id},"\n";
}
__DATA__
SITE BARCODE QTY SP CP 
10001 6281103890017 10 50 48
10001 6281103890031 2 12 10
10001 6281103890024 5 25 22
10002 6281103890017 13 50 48
10002 6281103890031 1 12 10
10002 6281103890024 2 25 22
10003 6281103890017 20 50 48
10003 6281103890031 4 12 10
10003 6281103890024 10 25 22

# 3  
Old 07-07-2009
hello Summer_Cherry...

Thanks for your reply but i am new to Unix scripting.
In the sample file, there are only three site's but normally i have around 12 site's and many barcode's for each site. May be, it was my mistake not making things clear when i posted at first. so is the code modifiable?
# 4  
Old 07-07-2009
The below script will append the values based on barcode. You may need to work on the header records.

Code:
 
val=`head -1 infile1`
grep -v "$val" infile1  >infile
while read record
do
a=`echo $record | cut -d " " -f2`
b=`echo $record | cut -d " " -f3-5`
x=`grep $a outfile | cut -d " " -f1`
if test "$x" -ne "$a"
then
echo $a $b >>outfile
else
sed "s/.*$x.*/& $b/" outfile >tmp
mv tmp outfile
fi
done<infile

# 5  
Old 07-07-2009
Probably summer_cherry has got your req wrong...
You can try this also..
Code:
awk ' {a[$2]=a[$2]" "$3" "$4" "$5} END {for (i in a) print i, a[i]}' inputfile

# 6  
Old 07-08-2009
Thanks Guys,

it gave me exactly as i wanted it but is it possible to arrange the column headers?

---------- Post updated 07-08-09 at 01:28 PM ---------- Previous update was 07-07-09 at 03:50 PM ----------

i just did the below for the header format, thanks guys for the support...

Code:
awk 'BEGIN {print "\t\t","10001","\t\t\t","10002","\t\t\t","10003","\n",
"BARCODE","\t","QTY","\t","SP","\t","CP","\t",
"QTY","\t","SP","\t","CP","\t","QTY","\t","SP","\t","CP"}
{if(NR==1) next; a[$2]=a[$2]"\t"$3"\t"$4"\t"$5} END {for (i in a) print i, a[i]}' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Merging rows based on same ID in First column.

Hellow, I have a tab-delimited file with 3 columns : BINPACKER.13259.1.p2 SSF48239 BINPACKER.13259.1.p2 PF13243 BINPACKER.13259.1.p2 G3DSA:1.50.10.20 BINPACKER.13259.2.p2 SSF48239 BINPACKER.13259.2.p2 PF13243 BINPACKER.13259.2.p2 G3DSA:1.50.10.20... (7 Replies)
Discussion started by: anjaliANJALI
7 Replies

2. Shell Programming and Scripting

Remove duplicate rows based on one column

Dear members, I need to filter a file based on the 8th column (that is id), and does not mather the other columns, because I want just one id (1 line of each id) and remove the duplicates lines based on this id (8th column), and does not matter wich duplicate will be removed. example of my file... (3 Replies)
Discussion started by: clarissab
3 Replies

3. Shell Programming and Scripting

Choosing rows based on column values

I have a .csv file: A,B,0.6 C,D,-0.7 D,E,0.1 A,E,0.45 D,G, -0.4 I want to select rows based on the values of the 3rd columns such that it is >=0.5 or <= -0.5 Thanks. A,B,0.6 D,G, -0.7 (1 Reply)
Discussion started by: Sanchari
1 Replies

4. Shell Programming and Scripting

Printing rows based on column range

Hello, I have a file with nearly 57K lines. I want to filter the lines based on the range of values in a column. For e.g. print lines whose 3rd filed is >=0.02. Input file: LOC_Os09g32030 LOC_Os02g18880 0.0200037219149773 undirected NA NA LOC_Os03g58630 LOC_Os09g35690 ... (1 Reply)
Discussion started by: Sanchari
1 Replies

5. UNIX for Dummies Questions & Answers

merging rows into new file based on rows and first column

I have 2 files, file01= 7 columns, row unknown (but few) file02= 7 columns, row unknown (but many) now I want to create an output with the first field that is shared in both of them and then subtract the results from the rest of the fields and print there e.g. file 01 James|0|50|25|10|50|30... (1 Reply)
Discussion started by: A-V
1 Replies

6. UNIX for Dummies Questions & Answers

Remove duplicate rows when >10 based on single column value

Hello, I'm trying to delete duplicates when there are more than 10 duplicates, based on the value of the first column. e.g. a 1 a 2 a 3 b 1 c 1 gives b 1 c 1 but requires 11 duplicates before it deletes. Thanks for the help Video tutorial on how to use code tags in The UNIX... (11 Replies)
Discussion started by: informaticist
11 Replies

7. Shell Programming and Scripting

join rows based on the column values

Hi, Please help me to convert the input file to a new one. input file: -------- 1231231231 3 A 4561223343 0 D 1231231231 1 A 1231231231 2 A 1231231231 4 D 7654343444 2 A 4561223343 1 D 4561223343 2 D the output should be: -------------------- 1231231231 3#1#2 A 4561223343 0 D... (3 Replies)
Discussion started by: vsachan
3 Replies

8. UNIX for Dummies Questions & Answers

Extracting rows from a text file based on the first column

I have a tab delimited text file where the first column can take on three different values : 100, 150, 250. I want to extract all the rows where the first column is 100 and put them into a separate text file and so on. This is what my text file looks like now: 100 rs3794811 0.01 0.3434 100... (1 Reply)
Discussion started by: evelibertine
1 Replies

9. Shell Programming and Scripting

how to delete duplicate rows based on last column

hii i have a huge amt of data stored in a file.Here in this file i need to remove duplicates rows in such a way that the last column has different data & i must check for greatest among last colmn data & print the largest data along with other entries but just one of other duplicate entries is... (16 Replies)
Discussion started by: reva
16 Replies

10. Shell Programming and Scripting

merge rows based on a common column

Hi guys, Please guide me if you have a solution to this problem. I have tried paste -s but it's not giving the desired output. I have a file with the following content- A123 box1 B345 bat2 C431 my_id A123 service C431 box1 A123 my_id I need two different outputs- OUTPUT1 A123... (6 Replies)
Discussion started by: smriti_shridhar
6 Replies
Login or Register to Ask a Question