Solved

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

Posted on 2010-08-30
2
270 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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
incorrect file reading in bash 7 143
Powershell to pull all Service accounts 1 78
Perl tar error 8 69
Linux  Script File Attributes 5 76
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

733 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