Find the answer to your Linux question:
Results 1 to 9 of 9
Hello everyone. I am trying to use perl or whatever programming lanaguage to do the following: Read a text file that has something like this in it: ISA*00* *00* *33*Test ...
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    4

    Perl Help With Text Manipulation

    Hello everyone. I am trying to use perl or whatever programming lanaguage to do the following:

    Read a text file that has something like this in it:

    ISA*00* *00* *33*Test *ZZ*1234567*SBR*P*18*ABC* 200739933 *19523222*M~

    I would like it to read the file and look for "SBR" and grab the ABC and place ABC after the "M~" with REF*23* in front of it. So it should look like this:

    ISA*00* *00* *33*Test *ZZ*1234567*SBR*P*18*ABC* 200739933 *19523222*M~REF*23*ABC

    In the file it will have multiple loop. If anyone can help out, I would greatly appreciate it. Thank you for looking.

  2. #2
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    If sed is acceptable:

    Code:
    sed 's/SBR\*[^*]*\*[^*]*\*\([^*]*\).*/&REF*23*\1/' filename
    Last edited by radoulov; 11-27-2007 at 07:51 PM. Reason: -1 character :)

  3. #3
    Linux Enthusiast apoorv_khurasia's Avatar
    Join Date
    Feb 2005
    Location
    Laurasia
    Posts
    624
    Perl:
    Code:
    while (<>)
    {
         if ($_ =~ m/SBR/)
         {
              $_ =~ s/^(.*)(ABC)(.*M~)/$1$3REF*23$2/;
              print $_, "\nFor testing only\n";
         }
    }
    Last edited by apoorv_khurasia; 11-27-2007 at 08:26 PM. Reason: REF*123 --> REF*23
    "There is no sixth rule"
    --Rob Pike
    Registered Linux User: 400426 home page

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by apoorv_khurasia View Post
    Perl:
    Code:
    while (<>)
    {
         if ($_ =~ m/SBR/)
         {
              $_ =~ s/^(.*)(ABC)(.*M~)/$1$3REF*23$2/;
              print $_, "\nFor testing only\n";
         }
    }
    if thats the case, this would suffice

    Code:
    ...
     if ($_ =~ m/SBR/)
    {
      print $_ . "REF*23*ABC" ;
    }
    ...
    don't think "ABC" is constant.

  5. #5
    Linux Enthusiast apoorv_khurasia's Avatar
    Join Date
    Feb 2005
    Location
    Laurasia
    Posts
    624
    Quote Originally Posted by ghostdog74 View Post
    if thats the case, this would suffice

    Code:
    ...
     if ($_ =~ m/SBR/)
    {
      print $_ . "REF*23*ABC" ;
    }
    ...
    Hmm....thats cool! Thanks.
    "There is no sixth rule"
    --Rob Pike
    Registered Linux User: 400426 home page

  6. #6
    Just Joined!
    Join Date
    Nov 2007
    Posts
    4
    Thank you everyone for the response. It is much appreciated. Yes ABC is not constant.

    Let me clarify a little bit more:

    ISA*00* *00* *33*Test *ZZ*1234567*SBR*P*18*ABC* 200739933 *DMG*D8*XXXXXXXX*M~

    to

    ISA*00* *00* *33*Test *ZZ*1234567*SBR*P*18*ABC* 200739933 *DMG*D8*XXXXXXXX*M~REF*23*ABC

    This is just one of many lines in the text. I guess I would need a loop. For ever SBR that it finds, it copies ABC (which is not constant), it places it after "DMG*D8*XXXXXXXX*M~" (XXXXXXX is always changing)

    After it does so, it creates a new file or replaces the file with the changes. I hope I'm clear on that. Once again, thank you very much for the help.

  7. #7
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Assuming the lines have an asterix as fieldseperator with the fields in the same order:

    Code:
    awk 'BEGIN {FS="*"}
    {
      if($10=="SBR") {
        print $0 "REF*23*" $13
      }
      else {
        print $0
      }
    }' file
    Regards

  8. #8
    Just Joined!
    Join Date
    Nov 2007
    Posts
    4
    * = Element
    : = Subelement
    ~ = segment

    so no the * is not a field seperator. Thanks.

  9. #9
    Just Joined!
    Join Date
    Nov 2007
    Posts
    4
    here is a record from the file:

    ISA*00* *00* *33*TEST *ZZ*201736437 *071002*1720*U*00401*000001534*0*T*:~GS*HC*837*201 736437*20071002*1720*1*X*004010X098A1~ST*837*0001~ BHT*0019*00*20071002172622*20071002*172622*CH~REF* 87*004010X098A1~NM1*41*2*TEST2*****46*TEST~PER*IC* JOHN DOE*EM*TEST@TEST.COM~NM1*40*2*ccn*****46*TEST3~HL* 1**20*1~NM1*85*2*SOUTHWEST AMBULANCE*****XX*1538116652~N3*PO BOX 12345~N4*PHOENIX*AZ*123456789~REF*EI*159656~HL*2*1 *22*1~SBR*P**001B12******ZZ~NM1*IL*1*TEST*TEST TEST****MI*123456789~N3*1212 TEST~N4*CASA GRANDE*AZ*85222~DMG*D8*19490926*M~Insert In hereNM1*PR*2*ELDORADO BPO SERVI*****PI*260837~HL*3*2*23*0~PAT*01~NM1*QC*1*TE ST*TEST TEST****MI*361429326~DMG*D8*19580107*F~CLM*1234567 8*1755.52***41::1*N*A*Y*N~REF*F8*26556566hm0006.ti f~REF*D9*ECI001B1220703825800~HI*BK:2931*BF:311~HC P*10*35.18~NM1*77*2*SOUTHWEST AMBULANCE~N3*UNKNOWN~N4*Casa Grande*AZ*99999~LX*1~SV1*HC:A0428*940.13*UN*1*41** 1~DTP*472*RD8*20070801-20070801~LX*2~SV1*HC:A0425*777.63*UN*49*41**1~DTP* 472*RD8*20070801-20070801~LX*3~SV1*HC:A0382*0.02*UN*1*41**1~DTP*472 *RD8*20070801-20070801~HCP*10*0.02~LX*4~SV1*HC:A0382*1.18*UN*1*4 1**1~DTP*472*RD8*20070801-20070801~HCP*10*1.18~LX*5~SV1*HC:A0382*0.16*UN*1*4 1**1~DTP*472*RD8*20070801-20070801~HCP*10*0.16~LX*6~SV1*HC:A0382*4.54*UN*1*4 1**1~DTP*472*RD8*20070801-20070801~HCP*10*4.54~LX*7~SV1*HC:A0382*0.03*UN*1*4 1**1~DTP*472*RD8*20070801-20070801~HCP*10*0.03~LX*8~SV1*HC:A0382*29.71*UN*1* 41**1~DTP*472*RD8*20070801-20070801~HCP*10*27.13~LX*9~SV1*HC:A0382*1.4*UN*4*4 1**1~DTP*472*RD8*20070801-20070801~HCP*10*1.4~LX*10~SV1*HC:A0382*0.72*UN*4*4 1**1~DTP*472*RD8*20070801-20070801~HCP*10*0.72~SE*69*0001~GE*1*1~IEA*1*00000 1534~

Posting Permissions

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