Solved

perl one liner

Posted on 2006-06-19
14
588 Views
Last Modified: 2008-01-09
I have a file that has groups of lines, I need to print out certain groups of lines if they contain a key word.
I usally do this with a one liner like.

perl -n00e 'print if /key_word/i' file1.txt

I need to do the same thing but use a text file that contains a long list of key words, how do I make x=(the key_words in file1.txt) then do something like

perl -n00e 'x=(key words in file1.txt;print if /$x/i' file2.txt


I tried something crazy like this but it prints out everything, not even close to what I'm trying to do.

for i in file1.txt; do perl -n00e 'print if /$i/i' file2.txt; done

Thanks,
0
Comment
Question by:bt707
  • 7
  • 3
  • 2
  • +2
14 Comments
 

Author Comment

by:bt707
Comment Utility
I tried to do a

cat file1.txt  then add a while (<>);

but still not getting it to work, how can I do this from a one liner and in a small script.

Thanks,
0
 
LVL 1

Expert Comment

by:FunnyMan
Comment Utility
Here's a script for you.  It takes two "files" of input, separated by an EOF marker (Ctrl-D on a blank line if doing it interactive).  Probably don't want to do it as a one-liner, but you could if you really wanted to.

$search = "(";
$first = true;
while ($line = <>)
{
  chomp($line);
  $search = "$search$line|";
}
chop($search);
$search = "$search)";
while ($line = <>)
{
  print $line if ($line =~ m/$search/i)
}

-FM
0
 
LVL 18

Expert Comment

by:Morcalavin
Comment Utility
Does it have to be done in perl?
for line in `cat file1.txt`; do grep -i $line file2.txt; done
The above line prints:
this is a test
please stop

file1 contains:
test
stop

file2 contains:
this is a test
do nothing
st
please stop
0
 

Author Comment

by:bt707
Comment Utility
hi Morcalavin,

for what I need I think I need perl, I'm not just doing a grep for a line that contains a keyword which is what your command would do, that would work fine for that but what I need is to pull a section out if it contians the key word.

In the file2.txt it contains sections with about 20 lines which each section is seperated by a blank line, the perl -00 let me read section in paragraph mode and if any of the sections contain a key word then it pulls out the whole section.

I can do it one at a time with  # perl -n00e 'print if /key_word/i' file2.txt    but want to be able to feed it multiple words to look for, the solution from FunnyMan may work but I can't seem to figure that one out.

Thanks,
0
 
LVL 18

Expert Comment

by:Morcalavin
Comment Utility
grep -p pulls out of paragraph using empty lines as delimiters:

for line in `cat file1.txt`; do grep -ip $line file2.txt; done
The above line prints:
this is a test line.  it should not end
here it should end

this is a stop line
here it should end



file2:
this is a test line.  it should not end
here it should end

this is a stop line
here it should end

hi there
why hello!
0
 

Author Comment

by:bt707
Comment Utility
where are you getting the -p option? I'm on a solaris 9 box, maybe that option with grep is on linux? not sure but I've never seen that option and don't have it.
Would be nice to have.
0
 
LVL 18

Expert Comment

by:Morcalavin
Comment Utility
I have it on my AIX boxes and Linux.

Your version of grep doesn't have a -p option?  What a drag.  You can use -p to separate at a blank line, or provide your own delimeter.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:bt707
Comment Utility
no don't have it here with Solaris, sure would be nice to have though.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
perl -ne 'if( $/ ){ local @ARGV=(); chomp(@_=<>); $x=join"|",@_; $x = qr/$x/i; $/=""; } print if /$x/' file1.txt file2.txt
0
 

Author Comment

by:bt707
Comment Utility
That worked ozo, but have one problem that I didn't think about, some of the groups of records contains the names that I'm searching for but do not want those records.

I only want the records if they contain the user name I'm looking for in file1.txt and it starts at the first of line one.  anytime the name is found in a record that I don't want it does not start the line so I just need to make it say the user names in file1.txt must start a line.

I tried changing to print if /^$x/ but obviously that didn't get what I needed. how can I print out only if the names in file1.txt are found at the begging of a line.

Thanks,
0
 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
Comment Utility
$x=join"|",map{"^$_"}@_; $x = qr/$x/is;
0
 

Author Comment

by:bt707
Comment Utility

I must of put something in wrong, it's printing the whole file2 and all on one line, here is what I tried.

perl -ne 'if( $/ ){ local @ARGV=(); chomp(@_=<>); $x=join"|",map{"^$_"}@_; $x = qr/$x/is; } print if /$x/' file1.txt file2.txt
0
 
LVL 16

Assisted Solution

by:manav_mathur
manav_mathur earned 100 total points
Comment Utility
You forgot the
$/="" ;
0
 

Author Comment

by:bt707
Comment Utility
Thanks manav, I finally did see where I had missed part of the command ozo gave, the command ozo posted all worked fine as usual, I had forgot to go back and close the question.

Thanks for pointing that out.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

10 Experts available now in Live!

Get 1:1 Help Now