The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
concatenate and display 2 lines as 1 with a condition for 2 line ? vithala Shell Programming and Scripting 7 07-11-2008 01:01 AM
Need solution concatenate and display 2 lines as 1 with a condition for 2 line ? vithala UNIX for Advanced & Expert Users 1 07-10-2008 01:27 PM
duplicate line in a text file nixguy Shell Programming and Scripting 5 04-29-2008 02:19 AM
removing line and duplicate line ocelot UNIX for Dummies Questions & Answers 11 01-30-2007 12:44 PM
how to concatenate two command in one line and get the display in one screen vasikaran UNIX for Dummies Questions & Answers 9 07-01-2005 05:41 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-29-2008
vaskarbasak vaskarbasak is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 22
Question concatenate all duplicate line in a file.

Hi All,

i have a zip file like the format

794051400123|COM|24|0|BD|R|99.98

794051413727|COM|11|0|BD|R|28.99

794051415622|COM|23|0|BD|R|28.99

883929004676|COM|0|0|BD|R|28.99
794051400123|MOM|62|0|BD|R|99.98

794051413727|MOM|4|0|BD|R|28.99

794051415622|MOM|80|0|BD|R|28.99

883929004676|MOM|0|0|BD|R|28.99

883929017164|MOM|0|0|BD|R|39.99
794051400123|RNO|73|0|BD|R|99.98

794051413727|RNO|8|0|BD|R|28.99

794051415622|RNO|84|0|BD|R|28.99

883929004676|RNO|0|0|BD|R|28.99
794051400123|SOM|25|0|BD|R|99.98

794051415622|SOM|80|0|BD|R|28.99

883929004676|SOM|0|0|BD|R|28.99

883929017164|SOM|0|0|BD|R|39.99
.................................

i need concate all duplicate line like

794051400123|COM|24|MOM|62|SOM|25|RNO|73
794051413727|COM|11||MOM|4|RNO|8
............
...

the file size is nearly 30 MB.So it takes lot of time.I have to do it with in 15 min.

please help me.

Thanks!
vaskar
  #2 (permalink)  
Old 04-29-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
What happened to the dollar amount at the end of the line?
  #3 (permalink)  
Old 04-29-2008
ShawnMilo ShawnMilo is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 252
Tested and working.

Code:
#!/usr/bin/env python

import sys

input = open(sys.argv[1], 'r')

dict = {}

for line in input:

    line = line.rstrip()

    if line.count("|"):
        line = line.split("|")
        if dict.has_key(line[0]):
            dict[line[0]].extend(line[1:3])
        else:
            dict[line[0]] = line[1:3]
            
input.close()

for key, value in dict.items():

    print("%s|%s" % (key, "|".join(value)))
  #4 (permalink)  
Old 04-30-2008
vaskarbasak vaskarbasak is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 22
I don't know python script.Is it possible write simple shell script? I have done it but it takes lot of time.
  #5 (permalink)  
Old 04-30-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Gift horse and all that ...

Assuming you mean take all values with an identical first field, and paste together fields 2 and 3 from all those lines, something like

Code:
sort file |
awk -F '|' '$1 == prev { collect=collect "|" $2 "|" $3; next }
{ if (collect) print collect; prev = $1; collect = $1 "|" $2 "|" $3; next }
END { if (collect) print collect }'
The sort might kill you if it's a very small or old system, but having sorted input makes the awk script very simple.
  #6 (permalink)  
Old 04-30-2008
vaskarbasak vaskarbasak is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 22
Thanks!! It is working....
  #7 (permalink)  
Old 08-25-2008
vaskarbasak vaskarbasak is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 22
Hi,

I am facing a big problem...It gives me wrong result

like

|COM|24|MOM|62|SOM|25|RNO|73
794051413727|COM|11||MOM|4|RNO|8

pls help me.

Thanks!
vaskar
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0