#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    17
    Rep Power
    0

    Exclamation Help with text editing in bash


    Dear all
    I have a text file which contains plenty of lines.In this file every two line is a group that should stay with each other.A part of my file is shown here:

    # 11 25 0
    MRD 80.0343 1 P

    # 12 16 0
    MRD 70.9987 1 P
    # 18 25 0
    MRD 79.6232 1 P

    # 13 3 0
    MRD 92.2977 1 P
    # 16 12 0
    MRD 80.4427 1 P
    # 10 13 0
    SHB 68.6062 1 P
    # 11 25 0
    SHB 25.7899 1 P

    # 10 24 0
    SHB 52.7862 1 P
    # 18 25 0
    SHB 70.7264 1 P

    # 11 25 0
    TBZ 62.0731 1 P

    # 13 11 0
    TBZ 9.8269 1 P
    # 18 25 0
    TBZ 62.6325 1 P


    I want to merge every group with the same first line. In other words to keep the first line and add the second line of the matched griups respectively like this:
    # 11 25 0
    MRD 80.0343 1 P
    SHB 25.7899 1 P
    TBZ 62.0731 1 P
    # 18 25 0
    MRD 79.6232 1 P
    SHB 70.7264 1 P
    TBZ 62.6325 1 P
    # 12 16 0
    MRD 70.9987 1 P
    # 13 3 0
    MRD 92.2977 1 P
    # 16 12 0
    MRD 80.4427 1 P
    # 10 13 0
    SHB 68.6062 1 P
    # 10 24 0
    SHB 52.7862 1 P
    # 13 11 0
    TBZ 9.8269 1 P

    As you can see every two line as a group should be compared according to their first line and if thats the same the second line of the groups are written under the key line.And if no matches occur we keep also that group in output file.
    I would appreciate any Help
    Thanks
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,365
    Rep Power
    1870
    A cryptic 1-line perl.
    Code:
    $ cat test1.txt
    # 11 25 0
    MRD 80.0343 1 P
    # 12 16 0
    MRD 70.9987 1 P
    # 18 25 0
    MRD 79.6232 1 P
    # 13 3 0
    MRD 92.2977 1 P
    # 16 12 0
    MRD 80.4427 1 P
    # 10 13 0
    SHB 68.6062 1 P
    # 11 25 0
    SHB 25.7899 1 P
    # 10 24 0
    SHB 52.7862 1 P
    # 18 25 0
    SHB 70.7264 1 P
    # 11 25 0
    TBZ 62.0731 1 P
    # 13 11 0
    TBZ 9.8269 1 P
    # 18 25 0
    TBZ 62.6325 1 P
    $ perl -e 'while(<>){chomp;if ( /^#/ ) { $k = $_; } else { push @{$l{$k}},$_; }} for $i ( keys %l ) { print "$i\n"; print join("\n",@{$l{$i}}) . "\n"; }' test1.txt
    # 18 25 0
    MRD 79.6232 1 P
    SHB 70.7264 1 P
    TBZ 62.6325 1 P
    # 11 25 0
    MRD 80.0343 1 P
    SHB 25.7899 1 P
    TBZ 62.0731 1 P
    # 12 16 0
    MRD 70.9987 1 P
    # 10 13 0
    SHB 68.6062 1 P
    # 13 11 0
    TBZ 9.8269 1 P
    # 13 3 0
    MRD 92.2977 1 P
    # 16 12 0
    MRD 80.4427 1 P
    # 10 24 0
    SHB 52.7862 1 P
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    17
    Rep Power
    0
    Dear Salem
    Many Thanks for your Help.I didn't know anything about perl




    Originally Posted by salem
    A cryptic 1-line perl.
    Code:
    $ cat test1.txt
    # 11 25 0
    MRD 80.0343 1 P
    # 12 16 0
    MRD 70.9987 1 P
    # 18 25 0
    MRD 79.6232 1 P
    # 13 3 0
    MRD 92.2977 1 P
    # 16 12 0
    MRD 80.4427 1 P
    # 10 13 0
    SHB 68.6062 1 P
    # 11 25 0
    SHB 25.7899 1 P
    # 10 24 0
    SHB 52.7862 1 P
    # 18 25 0
    SHB 70.7264 1 P
    # 11 25 0
    TBZ 62.0731 1 P
    # 13 11 0
    TBZ 9.8269 1 P
    # 18 25 0
    TBZ 62.6325 1 P
    $ perl -e 'while(<>){chomp;if ( /^#/ ) { $k = $_; } else { push @{$l{$k}},$_; }} for $i ( keys %l ) { print "$i\n"; print join("\n",@{$l{$i}}) . "\n"; }' test1.txt
    # 18 25 0
    MRD 79.6232 1 P
    SHB 70.7264 1 P
    TBZ 62.6325 1 P
    # 11 25 0
    MRD 80.0343 1 P
    SHB 25.7899 1 P
    TBZ 62.0731 1 P
    # 12 16 0
    MRD 70.9987 1 P
    # 10 13 0
    SHB 68.6062 1 P
    # 13 11 0
    TBZ 9.8269 1 P
    # 13 3 0
    MRD 92.2977 1 P
    # 16 12 0
    MRD 80.4427 1 P
    # 10 24 0
    SHB 52.7862 1 P
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    834
    Rep Power
    387

    Cool


    Originally Posted by sahar sa
    Dear Salem
    Many Thanks for your Help.I didn't know anything about perl
    Here is an awk version.
    Assuming your data is in a file called "file1.txt" then:
    Code:
    ==> cat my_script
    awk '/^#/{key=$0; continue}
    {k[key]=k[key]":"$0}
    END {
    for (key in k){printf"%s",key; n=split(k[key],m,":");
      for (i=1;i<=n;++i){print m[i]}}
    }' file1.txt
    
    ==> ./my_script
    # 11 25 0
    MRD 80.0343 1 P
    SHB 25.7899 1 P
    TBZ 62.0731 1 P
    # 18 25 0
    MRD 79.6232 1 P
    SHB 70.7264 1 P
    TBZ 62.6325 1 P
    # 13 11 0
    TBZ 9.8269 1 P
    # 12 16 0
    MRD 70.9987 1 P
    # 13 3 0
    MRD 92.2977 1 P
    # 16 12 0
    MRD 80.4427 1 P
    # 10 13 0
    SHB 68.6062 1 P
    # 10 24 0
    SHB 52.7862 1 P

IMN logo majestic logo threadwatch logo seochat tools logo