Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > Perl Help With Text Manipulation

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 11-27-2007   #1 (permalink)
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.
crazyperl is offline  


Reply With Quote
Old 11-27-2007   #2 (permalink)
Linux Newbie
 
radoulov's Avatar
 
Join Date: Sep 2007
Posts: 101
If sed is acceptable:

Code:
sed 's/SBR\*[^*]*\*[^*]*\*\([^*]*\).*/&REF*23*\1/' filename

Last edited by radoulov; 11-27-2007 at 08:51 PM.. Reason: -1 character :)
radoulov is offline   Reply With Quote
Old 11-27-2007   #3 (permalink)
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";
     }
}
__________________
"There is no sixth rule"
--Rob Pike
Registered Linux User: 400426 home page

Last edited by apoorv_khurasia; 11-27-2007 at 09:26 PM.. Reason: REF*123 --> REF*23
apoorv_khurasia is offline   Reply With Quote
Old 11-28-2007   #4 (permalink)
Linux User
 
Join Date: Aug 2006
Posts: 434
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.
ghostdog74 is offline   Reply With Quote
Old 11-28-2007   #5 (permalink)
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
apoorv_khurasia is offline   Reply With Quote
Old 11-28-2007   #6 (permalink)
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.
crazyperl is offline   Reply With Quote
Old 11-28-2007   #7 (permalink)
Linux Enthusiast
 
Franklin52's Avatar
 
Join Date: Aug 2006
Posts: 598
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
Franklin52 is offline   Reply With Quote
Old 11-28-2007   #8 (permalink)
Just Joined!
 
Join Date: Nov 2007
Posts: 4
* = Element
: = Subelement
~ = segment

so no the * is not a field seperator. Thanks.
crazyperl is offline   Reply With Quote
Old 11-28-2007   #9 (permalink)
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~
crazyperl is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 02:50 PM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2