The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem with regexp for IP-Adress Pattern desertchannel Shell Programming and Scripting 9 05-25-2008 11:37 PM
How to extract two words at the same time. Aejaz UNIX for Advanced & Expert Users 6 04-30-2008 06:09 AM
extract words with even numbr of letters manish205 Shell Programming and Scripting 3 02-19-2008 03:18 AM
Please help! Sed extract a pattern zhen Shell Programming and Scripting 11 09-18-2006 09:36 AM
getting file words as pattern matching arunkumar_mca High Level Programming 5 05-31-2005 12:28 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-20-2008
Registered User
 

Join Date: Jan 2006
Posts: 59
Unhappy Extract words before and after a pattern/regexp

Couldn't find much help on the kind of question I've here:
There is this text file with text as:

Line one has a bingo
Line two does not have a bingo but it has a tango
Bingo is on line three
Line four has both tango and bingo

Now I would want to search for the pattern "bingo" in this file and split the line to extract the words immediately preceding and following - "bingo".

Tried with this script using perl.. but my output file shows no result.

#! /usr/bin/perl

open (INFILE, "text1.txt");
open (OUTFILE,">outtext1.txt");

while (<INFILE>)
{
if (s/\w*(\w{1})bingo(\w{1})\w*/\1\2/) {
print OUTFILE;
}

}
close (INFILE);
close (OUTIFLE);

WIth this expression, I expect to get atleast the 2nd line to pass and the output file to have a but ['a bingo but']. But I get an empty outfile.

Can someone please point out how to accomplish this?

Thanks!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-20-2008
Registered User
 

Join Date: Jun 2007
Posts: 355
Hi,

I am not sure whether i have understood your req correcctly. Just try below awk script.

Code:
nawk '{
for(i=1;i<=NF;i++)
{
j=i+1
k=i-1
if($i=="bingo" && $j!="" && $k!="")
print $k" "$j
}
}' filename
Reply With Quote
  #3 (permalink)  
Old 03-20-2008
Registered User
 

Join Date: Jan 2006
Posts: 59
Thanks Summer cherry! You got my question and nawk does the work, but can't we get this done by writing a simple regular expression?

Thanks again!
Reply With Quote
  #4 (permalink)  
Old 03-21-2008
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 496
Hi.

Adjusting the RE in script file p1 to account for whitespace on either side of bingo, etc.:
Code:
#!/usr/bin/perl

use warnings;
use strict;

open( INFILE, "data1" ) || die " Can't open input file.\n";

while (<INFILE>) {
  if (s/.*(\w+)\s*bingo\s*(\w+).*/$1 $2/) {
    print;
  }

}

exit(0);
Producing:
Code:
% ./p1
a but
See man perlre for details ... cheers, drl
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:24 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0