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
Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Monitor Aliased network interface bandwitch CentOS 4 98
Shell Mail is not working 5 114
Sed question 2 102
Perl File::Find alternative 1 78
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…
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…

860 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