Merge data in lines from same file

Need help figuring out how to merge data from a file. I have a large txt file with some data that needs to be merged from separate lines into one line.

Doug.G|3/12/2011|817-555-5555|Portland
Doug.G|3/12/2011|817-555-5522|Portland
Steve.F|1/11/2007|817-555-5111|Portland
Eric.W|2/17/1999|817-555-5001|Seattle
Eric.W|2/17/1999|902-555-5999|Seattle
Eric.W|2/17/1999|718-444-5111|Seattle

I need to combine column 3 in a |#,#| format on lines that have a match on column 1.

Desired

Doug|3/12/2011|817-555-5555,817-555-5522|Portland
Steve|1/11/2007|817-555-5111|Portland
Eric|2/17/1999|817-555-5001,902-555-5999,718-444-5111|Seattle

I can awk and tell me there is a difference on each line but can not figure out how to perform the desired merged output. There are some entries where three lines as in Eric or none as Steve, if there where all dups I could sort and cut | merge on next line.

Hi, try:

awk '{i=$1 FS $2} !B[$0]++{A=A (A?",":x) $3; C=$4} END{for(i in A) print i,A,C}' FS=\| OFS=\| file

--

awk '
  {
    i=$1 FS $2                     # Set index to $1 "|" $2
  }
  !B[$0]++ {                       # Only execute if line has not occurred before. Discard duplicate lines, using independent array B 
    A=A (A?",":x) $3      # Add $3 to the string if A is not empty, the prepend a comma else prepend nothing (x) 
    C=$4                        # Add the fourth field to a entry with index i in separate array C
  }
  END {
    for(i in A) print i,A,C  # For all found indices where the entire line is no duplicate, print the index "|" concatenated field 3 "|" C
  }
' FS=\| OFS=\| file

Output:

Eric.W|2/17/1999|817-555-5001,902-555-5999,718-444-5111|Seattle
Doug.G|3/12/2011|817-555-5555,817-555-5522|Portland
Steve.F|1/11/2007|817-555-5111|Portland
1 Like

Hello cdubu2,

Could you please try following.

awk -F"|" 'FNR==NR{A[$1]=A[$1]?A[$1] "," $3:$3;next} ($1 in A){$3=A[$1];print;delete A[$1]}' OFS="|"   Input_file  Input_file

Thanks,
R. Singh

1 Like

Thank you for the quick replies. These all worked on my Ubuntu machine however none work on my Solaris system, must be a syntax difference.

Hello cdubu2,

On a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk , it should fly then.

Thanks,
R. Singh

1 Like

nawk did it, thank you Thank to all the replies!