Help with data rearrangement based on share same content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with data rearrangement based on share same content
# 1  
Old 05-30-2012
Help with data rearrangement based on share same content

Input file
Code:
data_2 USA
data_2 JAPAN
data_3 UK
data_4 Brazil
data_5 Singapore
data_5 Indo
data_5 Thailand
data_6 China

Desired output file
Code:
data_2 USA/JAPAN
data_3 UK
data_4 Brazil
data_5 Singapore/Indo/Thailand
data_6 China

I would like to merge all data content that share same "data_X" with "/" symbol.
Awk one-liner should be able to do it?

Many thanks for any advice.
# 2  
Old 05-30-2012
Hi


Code:
$ awk '{if (a[$1])a[$1]=a[$1]"/"$2;else a[$1]=$2;}END{for (i in a)print i, a[i];}' file
data_2 USA/JAPAN
data_3 UK
data_4 Brazil
data_5 Singapore/Indo/Thailand
data_6 China


Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 05-30-2012
How about this ?

Code:
#!/usr/bin/perl

%hash=();

while (<DATA>) {
        chomp;
        @flds=split;
        push(@{$hash{$flds[0]}},$flds[1]);
}
foreach (sort(keys %hash)) {
        print $_," ",join ("/",@{$hash{$_}}),"\n";
}


__DATA__
data_2 USA
data_2 JAPAN
data_3 UK
data_4 Brazil
data_5 Singapore
data_5 Indo
data_5 Thailand
data_6 China

This User Gave Thanks to pravin27 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to print the line that share exactly same with column one content

Input file : AAAG TC AACCCT AACCCT AACCCT AACCCT TCTG TCTG TCTG AC AC TCTG TCTG AC AC AC AC AC AGTG AC AGTG TCC Desired output file : AACCCT AACCCT AACCCT AACCCT AC AC I would like to print out the line that share exactly same as the first column data content. Column one data... (4 Replies)
Discussion started by: perl_beginner
4 Replies

2. Shell Programming and Scripting

Help with keep the record with share content

Input file: 1234 USA date 3421 USA date 3421 USA content 1234 USA1 date 34 USA1 content 1234 USA2 Sun 34 USA2 Sun 43 USA2 Sun 345 USA2 date 435 USA2 date1 Output file: 1234 USA date 3421 USA date 1234 USA1 date 1234 USA2 Sun 34 USA2 Sun 43 USA2 Sun (0 Replies)
Discussion started by: perl_beginner
0 Replies

3. Shell Programming and Scripting

Help with analysis data based on particular column content

Input file: Total_counts 1306726155 100% Number_of_count_true 855020282 Number_of_count_true_1 160014283 Number_of_count_true_2 44002825 Number_of_count_true_3 18098424 Number_of_count_true_4 24693745 Number_of_count_false 115421870 Number_of_count_true 51048447 Total_number_of_false ... (2 Replies)
Discussion started by: perl_beginner
2 Replies

4. Shell Programming and Scripting

Data reformat and rearrangement problem asking

Input file: dependent general_process dependent general_process regulation general_process - - template component food component binding data_rearrangement binding data_rearrangement specific_activity data_rearrangement - ... (7 Replies)
Discussion started by: cpp_beginner
7 Replies

5. Shell Programming and Scripting

Help with data reformat if share share content

Input data: read1_data1 read1_data1 read2_data1 read3_data1 read4_data1 read4_data1 read4_data1 read5_data1 . . Desired output result: read1_data1 read1_data2 read2_data1 read3_data1 read4_data1 (3 Replies)
Discussion started by: perl_beginner
3 Replies

6. Shell Programming and Scripting

Rearrangement of data content problem

Input data: >sample_1 WETYUPVLGK DGGHHHWETY QPERTTGGLO >sample_2 WRRTTOOLLP MKMKNJUTYE DLGLTTOC . . Desired output: >sample_1 WETYUP VLGKDG GHHHWE (8 Replies)
Discussion started by: patrick87
8 Replies

7. Shell Programming and Scripting

Change file content based on data

I have a Transaction File coming into the system. In this file, in all records the relevant data is as follows- Position 1:10 -> Transaction Code Position 252:255 -> 4 digit business code Now based on these 2 fields I have to alter value in Transaction code (Position 1:10)... (6 Replies)
Discussion started by: varunrbs
6 Replies

8. Shell Programming and Scripting

Extract specific data content from a long list of data

My input: Data name: ABC001 Data length: 1000 Detail info Data Direction Start_time End_time Length 1 forward 10 100 90 1 forward 15 200 185 2 reverse 50 500 450 Data name: XFG110 Data length: 100 Detail info Data Direction Start_time End_time Length 1 forward 50 100 50 ... (11 Replies)
Discussion started by: patrick87
11 Replies

9. Shell Programming and Scripting

Extract data based on match against one column data from a long list data

My input file: data_5 Ali 422 2.00E-45 102/253 140/253 24 data_3 Abu 202 60.00E-45 12/23 140/23 28 data_1 Ahmad 256 7.00E-45 120/235 140/235 22 data_4 Aman 365 8.00E-45 15/65 140/65 20 data_10 Jones 869 9.00E-45 65/253 140/253 18... (12 Replies)
Discussion started by: patrick87
12 Replies
Login or Register to Ask a Question