Sponsored Content
Top Forums Shell Programming and Scripting PERL: matching text between 2 values Post 302838695 by chris01010 on Tuesday 30th of July 2013 09:15:45 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
match(1)                                                        Mail Avenger 0.8.3                                                        match(1)

NAME
match - Match strings against glob paterns SYNOPSIS
match [-gilrqs] [-n <n>] [-c cmd] [-x code] {[-p] pattern | -f <file>} str1 [str2 ...] DESCRIPTION
match checks strings against pattern, which should be a shell-like glob pattern. pattern may contain the following special characters: ? A "?" character in pattern matches any single character in the string, except that the "/" character is only matched if match was given the -s option. * A "*" character in pattern matches zero or more characters in the string. The exception is that it will only match "/" characters if match was given the -s option. [...] A set of characters between square brackets matches any character in the set. In addition, the "-" character can be used to specify a range. For example "[+e0-3]" would match any of the characters "+", "e", 0, 1, 2, or 3 in the input string. To include a hyphen ("-") in the set of characters matched, either include the hyphen first or last, or escape it with a "". [!...] A character class preceded by a "!" matches any character but those specified in the class. The exception is that the negated character class will match a "/" only if match was given the -s option. c The backslash character escapes the next character c. Thus, to match a literal "*", you would use the pattern "*". match prints each string that matches pattern, one per line, and exits 0 if one or more strings matched. If no string matches, match exits with status 67 (or whatever alternate status was specified by the -x flag). If the -n n flag was specified, match prints only the text that matched the nth occurrence of "*" in the patten. OPTIONS -f file Specifies that the pattern should be read from file. match will read each line of the file and consider it as pattern to match against the argument strings. For each argument string, match stops when it hits the first matching line of the file. If file does not exist, match exits 67, or whatever code was specified by -x. -g Normally, the -n option selects text matching particular "*" characters in the patern. -g changes this behavior to use parentheses for grouping. Thus, for instance, the text "foo.c" would match pattern "*(.[ch])", and the output with option -n 1 would be ".c". To include a literal "(" or ")" in the pattern with the -g option, you must precede the character with a "". -i Makes the match case insensitive. str will be considered to match if any variation on its capitalization would match. For example, string "G" would match pattern "[f-h]". -l When a pattern matches the string in more than one way, the -l flag says to assign as much text as possible to the leftmost "*"s in the pattern. For example, pattern "*+*" would match text "a+b+c", and the first "*" would match "a+b". This behavior is the default, thus -l's effect is only to undo a previous -r flag. -n n With this flag, match prints the text that matched the nth "*" in the pattern, as opposed to printing the whole string. The leftmost "*" corresponds to -n 1. Specifying -n 0 causes match to print the whole matching string. Specifying -n -1 or using a value greater than the number of "*"s in the pattern causes match not to print anything, in which case you can still use the exit status to see if there is a match. The default value for n is 0, unless -g has also been specified, in which case the default is 1. -c command When -c is specified, match runs command with the system shell (/bin/sh), giving it as argument $0 the full string that matched, and as arguments $1, $2, etc., the parts of the string that matched any "*"s in pattern. If the command does not exit with status 0, match will exit immediately, before processing further matches, with whatever status command returned. The -c and -n flags are mutually exclusive. -p pattern Specifies the pattern to match against. The -p flag is optional; you can specify pattern as the first argument following the options. However, if you want to try matching the same input string against multiple patterns, then you must specify each pattern with a -p flag. -q This option is synonymous with -n -1; it suppresses output when there is a match. You can still determine whether a match occurred by the exit status. -r When a pattern matches the string in more than one way, the -r flag says to assign as much text as possible to the rightmost "*"s in the pattern. For example, with -r, pattern "*+*" would match text "a+b+c" with the "*" matching "a", and the second matching "b+c". -s Ordinarily, "*", "?", and negated character classes ("[!...]") do not match "/" characters. -s changes this behavior to match slashes. -x code By default, when there is no match, match exits with status 67. With this option, match exits with status code, instead. EXAMPLES
Suppose you have a directory with a bunch of files ending .c and .o. If, for each file named foo.c you want to attempt to delete the file foo.o, you can run the following command: match -p '*.c' -c 'rm -f $1.o' *.c Servers running the mailman list manager often send mail from bounce addresses of the form listname-bounces@host.com. If you subscribe to multiple lists on the same server, the mailman interface makes it easier if you subscribe under the same address. To split the mail into multiple folders based on the bounce address in the environment variable SENDER, you might chose a mailbox with the following shell code: name=`match -n1 "*-bounces@host.com" "$SENDER"` && echo "$HOME/Mail/incoming/host-$name.spool" SEE ALSO
avenger(1), avenger.local(8) The Mail Avenger home page: <http://www.mailavenger.org/>. AUTHOR
David Mazieres Mail Avenger 0.8.3 2012-04-05 match(1)
All times are GMT -4. The time now is 04:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy