Solved

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

759 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

17 Experts available now in Live!

Get 1:1 Help Now