Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Utilizing an array to gracefully append to a list of EmailAddresses
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…
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now