Urgent : Merge similar lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Urgent : Merge similar lines
# 1  
Old 09-08-2008
Bug Merge similar lines

Hi,

I have a file like this.

Quote:
./usr/orders/order_2628 identical to the existing
./usr/orders1/order_new_2627 to the existing
./usr/orders1/order_new_2627 from the existing
./usr/orders/order_2652 The name of the object
./usr/orders1/order_new_2627 new order
please notice that ./usr/orders1/order_new_2627 appears more than once, thus needs to be merged.

I would like to merge the lines where the first column match

so the output should be like this:
Quote:
./usr/orders/order_2628 identical to the existing
./usr/orders1/order_new_2627 to the existing:from the existing:new order
./usr/orders/order_2652 The name of the object
Please help

Last edited by rakeshou; 09-09-2008 at 12:20 AM..
# 2  
Old 09-08-2008
Java

<rant mode>
It's not a UNIX.com rule at present, but personally I really dislike seeing 'URGENT' in the subject without any explanation of why Smilie
</rant mode>

That said, I see you've also been active in helping others so I should probably just calm down Smilie

Not a very elegant solution but it should do the job (note that this will go all wrong if you have any spaces in the "/usr/orders1/order..." names in your list).
Code:
#!/usr/bin/perl -w
my %lines;
while (<>) {
  if (/^\s*([^\s]+) (.*)$/) {
    $lines{$1}.="$lines{$1}:$2";
  }
}
foreach $key keys(%lines) {
  print "$key $lines{$key}\n";
}

(Untested)
# 3  
Old 09-10-2008
Code:
awk '{
arr[$1]=sprintf("%s:%s",arr[$1],substr($0,length($1)+1))
}
END{
for ( i in arr)
        print i" " arr[i]
}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash/shell merge similar lines

Hello, I've been working on a bash script to parse through firewall logs (cisco). I'm nearing the end and have a dilemma. My data looks as such (actual data is several gigs worth of logs - without the headers): sourceIP destinationIP destinationProtocol destinationPort 1.1.1.1 2.2.2.2 ... (2 Replies)
Discussion started by: umang2382
2 Replies

2. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

3. Shell Programming and Scripting

Merge two files with similar column entries

Hi , I have few files which contains user name and data transfer rate in MBs and this data is collected for year and for each month report is saved in 12 different files I have to merge all the files to prepare the final report Files are as below Similarly I have 10 more files ... (5 Replies)
Discussion started by: pratapsingh
5 Replies

4. Shell Programming and Scripting

Help with merge two file based on similar column content

Input file 1: A1BG A1BG A1BG A1CF A1CF BCAS BCAS A2LD1 A2M A2M HAT . . Input file 2: A1BG All A1CF TEMP (5 Replies)
Discussion started by: perl_beginner
5 Replies

5. UNIX for Dummies Questions & Answers

merge lines within a file that start with a similar pattern

Hello! i have a text file.. which contains the data as follows i want to merge the declarations lines pertaining to one datatype in to a single line as follows i've searched the forum for help.. but couldn't find much help.. how can i do this?? (1 Reply)
Discussion started by: a_ba
1 Replies

6. UNIX for Dummies Questions & Answers

Merge two files with two columns being similar

Hi everyone. How can I merge two files, where each file has 2 columns and the first columns in both files are similar? I want all in a file of 4 columns; join command removes the duplicate columns. 1 Dave 2 Mark 3 Paul 1 Apple 2 Orange 3 Grapes to get it like this in the 3rd file:... (9 Replies)
Discussion started by: Atrisa
9 Replies

7. Shell Programming and Scripting

remove blank lines and merge lines in shell

Hi, I'm not a expert in shell programming, so i've come here to take help from u gurus. I'm trying to tailor a csv file that i got to make it work for the LOAD FROM command. I've a datatable csv of the below format - --in file format xx,xx,xx ,xx , , , , ,,xx, xxxx,, ,, xxx,... (11 Replies)
Discussion started by: dvah
11 Replies

8. Shell Programming and Scripting

merge similar rows

I have a large file (10M lines) that contains two columns: a frequency and a string, ex: 3 aaaaa 4 bbbbb 2 ccccc 5 aaaaa 1 ddddd 4 ccccc I need to merge the lines whose string part is the same, while updating the frequency. The output should look like this: 8 aaaaa 4 bbbbb 5 ccccc... (2 Replies)
Discussion started by: tootles564
2 Replies

9. Shell Programming and Scripting

merging similar lines

Greetings, I have been trying to merge the following lines: Sat. May 9 8:00 PM Sat. May 9 8:00 PM CW Sat. May 9 8:00 PM CW Cursed Sat. May 9 9:00 PM Sat. May 9 9:00 PM CW Sat. May 9 9:00 PM CW Sanctuary Sat. May 16 8:00 PM Sat. May 16 8:00 PM CW Sat. May 16 8:00 PM CW Sanctuary Sat. May... (2 Replies)
Discussion started by: adambot
2 Replies

10. Shell Programming and Scripting

Urgent help needed on merging lines with similar words

Hi everyone, I need help with a merging problem. Basically, I have a file with several lines (in this example 9 lines) such as: Amie, Jay, Sasha, Rob, Kay Mia, Frank Jay, Nancy, Cecil Paul, Ked, Nancy, 17, Fred 14, 16, 18, 20 9, 11 12, Frank 18, Peter, 62 Nancy, 27 A delimiter is... (3 Replies)
Discussion started by: awb221
3 Replies
Login or Register to Ask a Question