CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Searching for text

 



Goldthorn
New User

Jan 6, 2016, 4:48 AM

Post #1 of 4 (1116 views)
Searching for text Can't Post

I want to find a word in a file, and print the entire lines that the word appears on.

I've got this far.

perl -lne 'print $1 if /(word)/' file

But this only finds the word and prints it. Not exactly what I want. Can anyone advise please?


FishMonger
Veteran / Moderator

Jan 6, 2016, 6:42 AM

Post #2 of 4 (1110 views)
Re: [Goldthorn] Searching for text [In reply to] Can't Post

Remove the $1


Code
perl -lne 'print if /word/' file



Goldthorn
New User

Jan 6, 2016, 7:35 AM

Post #3 of 4 (1106 views)
Re: [FishMonger] Searching for text [In reply to] Can't Post

I knew I was doing something stupid. Thanks! :D

I'm wondering if there's a way to search for the date. So if there was a date at the beginning of each line, do a search for a particular date and then the word.


Laurent_R
Veteran / Moderator

Jan 7, 2016, 6:29 AM

Post #4 of 4 (1081 views)
Re: [Goldthorn] Searching for text [In reply to] Can't Post

I would say that you should look for the date only if the searched word is there: no point of trying to isolate the date if the word is not there.

As for finding the date, there are many many ways and formats a date may be written. You should give an example of your line so that we can help finding the date.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives