?
Solved

SED output before and after file enclose keywords in quotes and d

Posted on 2010-08-30
2
Medium Priority
?
290 Views
Last Modified: 2012-06-27
Hi,

I have a large keyword list that looks like this:

aaron ross who is he dating
aaron spelling dating
aaron t jeffries dating profile
aarp dating service
a basic guide to dating edwin m knowles china
abbreviation for ipa in dating
abbreviations gl dating
abby s joplin premier dating
abby wambach and christie rampone dating
abc simple rules for dating my teenage daughter
abc s of dating
abdl dating
abe dating software
aberdeen wa dating news
abesha women dating
abislty tolal free dating site
abk soft ltd dating and community
able dating
abledating
able dating demo
abledating demo
abledating helpdesd
abledating helpdesk
abledating nulled download
abledating reviews
abledating russian
abledating scam
able dating security
abledatingsites
able dating software
abledating software
abledating spam

I'm looking for a way where i can output one word before pattern "dating" and one after. In case dating is the first then 2 words following pattern match. In case last, two words preceding. Any help would be appreciated. thanks in advance.
0
Comment
Question by:faithless1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 2000 total points
ID: 33564169
This should do what you want...

Let me know if it doesn't match what you expect in some way.
#!/usr/local/bin/perl
use strict;
use warnings;

# to use a file/text "piped" in on STDIN, change while to:
# while (<>) {
# to use an external file, change while to:
# open IN, '<file' or die "could not read file: $!";
# while (<IN>) {
while (<DATA>) {
    chomp;
    if (m{(\w+\s+dating\s+\w+|^dating\s+\w+\s+\w+|\w+\s+\w+\s+dating$)}) {
        my $match = $1;
        $match =~ s{^dating\s+|\s+dating}{};
        print "got(1): $match\n";
# separated because I'm not clear if catching these lines is desirable
    } elsif (m{(\w+dating\s+\w+|\w+\s+dating\w+|\w+\s+\w+dating$|^dating\w+\s+\w
+)}) {
        my $match = $1;
        $match =~ s{dating}{};
        print "got(2): $match\n";
    } elsif (m{(\w+dating\w+)}) {
        my $match = $1;
        $match =~ s{dating}{ };
        print "got(3): $match\n";
    } else {
        warn "line $. did not match expected pattern: $_\n";
    }
}

__DATA__
aaron ross who is he dating
aaron spelling dating
aaron t jeffries dating profile
aarp dating service
a basic guide to dating edwin m knowles china
abbreviation for ipa in dating
abbreviations gl dating
abby s joplin premier dating
abby wambach and christie rampone dating
abc simple rules for dating my teenage daughter
abc s of dating
abdl dating
abe dating software
aberdeen wa dating news
abesha women dating
abislty tolal free dating site
abk soft ltd dating and community
able dating
abledating
able dating demo
abledating demo
abledating helpdesd
abledating helpdesk
abledating nulled download
abledating reviews
abledating russian
abledating scam
able dating security
abledatingsites
able dating software
abledating software
abledating spam

Open in new window

0
 

Author Comment

by:faithless1
ID: 33573172
Thank you! I will try this now to see what I get.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Six Sigma Control Plans

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question