Find the answer to your Linux question:
Results 1 to 7 of 7
I have a lot of records like these 4 records in a file. 1030082152000009bd96|2|563391238|1|20070806082223|-652|0|0|367408|366769|0|41|2|0| 1030084902000009bf3b|2|783355266|1|20070806084933|-782|0|0|17892|17253|0|47|2|0| 1030085205000009bf72|2|683355266|1|20070806085236|-189|0|0|27477|26838|0|47|2|0| 1030082152000009bd96|2|563391238|1|20070906090310|-652|0|0|367408|366769|0|41|2|0| The 1st and the last record have the same 1st field. I need ...
  1. #1
    Just Joined!
    Join Date
    Nov 2008
    Posts
    6

    Need to join similar records based on a column value

    I have a lot of records like these 4 records in a file.

    1030082152000009bd96|2|563391238|1|20070806082223|-652|0|0|367408|366769|0|41|2|0|
    1030084902000009bf3b|2|783355266|1|20070806084933|-782|0|0|17892|17253|0|47|2|0|
    1030085205000009bf72|2|683355266|1|20070806085236|-189|0|0|27477|26838|0|47|2|0|
    1030082152000009bd96|2|563391238|1|20070906090310|-652|0|0|367408|366769|0|41|2|0|

    The 1st and the last record have the same 1st field. I need to join the two records together in a single record based on this. For every 1st field value there are 2 records in the file.

    Can anyone please help me with this?

    Thanks

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    What do you mean by 'join' them. Anyway, I'd 1st sort the file so the records would be next to each other:

    Code:
    # sort -t"|" -k1,1

  3. #3
    Just Joined!
    Join Date
    Nov 2008
    Posts
    6
    I need to get the two records together into a single line to compare the values of the 6th field.

  4. #4
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    If you just post an example of the desired output it should be easier.

  5. #5
    Just Joined!
    Join Date
    Nov 2008
    Posts
    6
    It's something like this.

    I have a list of records in a file one in each line of the file. Something like this.

    1030082152000009bd96|2|563391238|1|20070806082223|-652|0|0|367408|366769|0|41|2|0|
    1030084902000009bf3b|2|783355266|1|20070806084933|-782|0|0|17892|17253|0|47|2|0|
    1030085205000009bf72|2|683355266|1|20070806085236|-189|0|0|27477|26838|0|47|2|0|
    1030082152000009bd96|2|563391238|1|20070906090310|-652|0|0|367408|366769|0|41|2|0|

    The 1st field is a sort of a key. For some records the the key is the same (like in the 1st and 4th records). I need to join the records with the same key together in the same line so that I get something like this.

    1030082152000009bd96|2|563391238|1|20070806082223|-652|0|0|367408|366769|0|41|2|0|_
    1030082152000009bd96|2|563391238|1|20070906090310|-652|0|0|367408|366769|0|41|2|0|

  6. #6
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Use nawk or /usr/xpg4/bin/awk on Solaris:

    Code:
    awk -F\| 'END {
      for (k in _) print _[k]
      }
    { _[$1] = _[$1] ? _[$1] $0 : $0 }
    ' infile

  7. #7
    Just Joined!
    Join Date
    Nov 2008
    Posts
    6
    It works Thanks a lot...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...