PERL: matching text between 2 values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL: matching text between 2 values
# 1  
Old 07-30-2013
PERL: matching text between 2 values

Hi,

I am trying to get a text value between 2 words in a string and assign it to a value. Basically the program should read each row in a file and return the text between 2 fields and print it to another file.

My code:

Code:
#!/usr/bin/perl
open FAILED, "./AFile.txt";
while(<FAILED>)
{
      local $/ = undef;
      $var = $_;
 
}
      ($var) =~ m/OPT_APP_GRP(.*)OPT_MSG_TYPE/s;
      print "$var\n\n";
      close FAILED;
      open FILE, ">./output\n";
      print FILE "$var\n"; close FILE;

the file to be read:

Code:
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_941A_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_941A_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_940A_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_940A_IC_0
  NNS_CLASS_IND R OPT_APP_GRP SWIFT OPT_MSG_TYPE S
  NNS_CLASS_IND R OPT_APP_GRP SWIFT OPT_MSG_TYPE S
  NNS_CLASS_IND R OPT_APP_GRP SWIFT OPT_MSG_TYPE S
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0

It currently returns the whole first line only!!

Code:
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_941A_IC_0

Any ideas?

Thanks.
# 2  
Old 07-30-2013
The value you want is stored in $1, not in $var after you do the regex.
# 3  
Old 07-30-2013
Subbeh's observation is correct, however you also probably want to apply the regex to all occurrences in $var, also either slurp the file (undefine the record separator $/) or step through the file while(<FAILED>){...}, not both.
Code:
open FAILED, './AFile.txt';
open FILE, '>', './output'; 

while(<DATA>)
{
      $_ =~ m/OPT_APP_GRP(.*)OPT_MSG_TYPE/gs;
            print FILE "$1\n";
}
close FAILED;
close FILE;

# 4  
Old 07-30-2013
And in case you wanted to slurp the entire file into a variable and process that variable to extract the words of interest, you could do it either in list context like so -

Code:
# !/usr/bin/perl
{                                                 # start a local block
  local $/ = undef;                               # $/ is set only within this block
  open(FAILED, "<", "./afile.txt");               # open input file for reading
  $var = <FAILED>;                                # slurp entire file, assign to $var
  close FAILED;                                   # close file
}                                                 # local block closed, $/ is reset now
@list = $var =~ /OPT_APP_GRP (.*) OPT_MSG_TYPE/g; # extract words of interest repeatedly and assign to array @list
open(FILE, ">", "./output");                      # open output file for writing
print FILE join("\n", @list),"\n";                # print array elements, one per line
close FILE;                                       # and close the file


or scalar context, like so -

Code:
# !/usr/bin/perl
{                                                  # start a local block
  local $/ = undef;                                # $/ is set only within this block
  open(FAILED, "<", "./afile.txt");                # open input file for reading
  $var = <FAILED>;                                 # slurp entire file, assign to $var
  close FAILED;                                    # close file
}                                                  # local block closed, $/ is reset now
open(FILE, ">", "./output");                       # open output file for writing
while ($var =~ /OPT_APP_GRP (.*) OPT_MSG_TYPE/g) { # loop over all words of interest and assign them to $1 per iteration
  print FILE $1,"\n";                              # print the word to the output file
}                                                  # we are done looping
close FILE;                                        # so close the output file

A test run of the first program follows:

Code:
$
$
$ cat afile.txt
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_941A_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_941A_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_940A_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_940A_IC_0
  NNS_CLASS_IND R OPT_APP_GRP SWIFT OPT_MSG_TYPE S
  NNS_CLASS_IND R OPT_APP_GRP SWIFT OPT_MSG_TYPE S
  NNS_CLASS_IND R OPT_APP_GRP SWIFT OPT_MSG_TYPE S
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
OPT_APP_GRP FROM_TANDEM OPT_MSG_TYPE A_OCEP_IC_0
$
$
$ cat -n process_afile.pl
     1  # !/usr/bin/perl
     2  {                                                 # start a local block
     3    local $/ = undef;                               # $/ is set only within this block
     4    open(FAILED, "<", "./afile.txt");               # open input file for reading
     5    $var = <FAILED>;                                # slurp entire file, assign to $var
     6    close FAILED;                                   # close file
     7  }                                                 # local block closed, $/ is reset now
     8  @list = $var =~ /OPT_APP_GRP (.*) OPT_MSG_TYPE/g; # extract words of interest repeatedly and assign to array @list
     9  open(FILE, ">", "./output");                      # open output file for writing
    10  print FILE join("\n", @list),"\n";                # print array elements, one per line
    11  close FILE;                                       # and close the file
$
$
$ cat output
cat: output: No such file or directory
$
$ perl process_afile.pl
$
$ cat output
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
SWIFT
SWIFT
SWIFT
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
FROM_TANDEM
$
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to merge two files with unique values matching.?

I have one script as below: #!/bin/ksh Outputfile1="/home/OutputFile1.xls" Outputfile2="/home/OutputFile2.xls" InputFile1="/home/InputFile1.sql" InputFile2="/home/InputFile2.sql" echo "Select hobby, class, subject, sports, rollNumber from Student_Table" >> InputFile1 echo "Select rollNumber... (3 Replies)
Discussion started by: Sharma331
3 Replies

2. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

3. Shell Programming and Scripting

Grep text matching problem with script which checks if web page contains text.

I wrote a Bash script which checks to see if a text string exists on a web page and then sends me an email if it does (or does not e.g. "Out of stock"). I run it from my crontab, it's quite handy from time to time and I've been using it for a few years now. The script uses wget to download an... (6 Replies)
Discussion started by: gencon
6 Replies

4. Shell Programming and Scripting

awk help - matching a field with certail values

Hello there, I have a file with few fields separated by ":". I wrote a below awk to manipulate this file: awk 'BEGIN { FS=OFS=":" }\ NR != 1 && $2 !~ /^98/ && $8 !~ /^6/{print $0}' $in_file > $out_file What I wanted was that if $8 field contains any of the values - 6100, 6110, 6200 -... (2 Replies)
Discussion started by: juzz4fun
2 Replies

5. Shell Programming and Scripting

How to print in awk matching $1 values ,to $1,$4 example given.?

Hi Experts, I am trying to get the output from a matching pattern but unable to construct the awk command: file : aa bb cc 11 dd aa cc 33 cc 22 45 68 aa 33 44 44 dd aa cc 37 aa 33 44 67 I want the output to be : ( if $1 match to "aa" start of the line,then print $4 of that line, and... (3 Replies)
Discussion started by: rveri
3 Replies

6. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

7. Shell Programming and Scripting

Hidden Characters in Regular Expression Matching Perl - Perl Newbie

I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense. ... (4 Replies)
Discussion started by: kittyluva2
4 Replies

8. Shell Programming and Scripting

Perl matching values in a file

I am trying to match first 5 values of $passt variable in a file $chpdvlst and then verify that value in $grp parameter I want first 5 values which has $vaht - ship and $grp = N-grp. Somehow script below lists whole file. $memcnt = 5; $passt = ship; open (ADVOUT, "< $chpdvlst") || die... (5 Replies)
Discussion started by: dynamax
5 Replies

9. Shell Programming and Scripting

Matching multiple values in a string

I've been battling with parsing a comma-delimited string, and have had what I would call B- success. I'm using perl and trying to parse out specific identifiers from a string, into a new string. When things are "normal," my regex works fine. When things get complicated, my script fails... (1 Reply)
Discussion started by: linber2880
1 Replies

10. Shell Programming and Scripting

matching multiple values in awk

How will you change the 5th column in the data file with the value in the second column in the error_correction.txt file. You have to match an extra variable, column 3 of the error_correction file with column 6 of the data.txt file. data.txt: vgr,bugatti veron,,3.5,Maybe,6,.......,ax2,....... (0 Replies)
Discussion started by: VGR
0 Replies
Login or Register to Ask a Question