Unix questions - Searching a pattern into specific files

This is Interesting: Free IT Magazines  
Home > Archive > Unix questions > August 2006 > Searching a pattern into specific files





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Searching a pattern into specific files
axel.orduna@gmail.com

2006-08-09, 1:26 am

I want to search a string pattern in mulitple files. I have a file
with a list of file names to search "fromfile", the string is "context"
ie:
Contents of "fromfile"
/src/exa100.c
/src/exae49.c
/inc/flastxx.c

I tried
awk '/context/ {print NR,$0}' < fromfile

But It don't work since it looks for the the string in fromfile and not
from the files listed whitin.

I appreciate your help!

Comcast News

2006-08-09, 1:26 am

[This followup was posted to comp.unix.questions]

While wandering through cyberspace on 8 Aug 2006 17:28:30 -0700, said
....
> I want to search a string pattern in mulitple files. I have a file
> with a list of file names to search "fromfile", the string is "context"
> ie:
> Contents of "fromfile"
> /src/exa100.c
> /src/exae49.c
> /inc/flastxx.c
>
> I tried
> awk '/context/ {print NR,$0}' < fromfile
>
> But It don't work since it looks for the the string in fromfile and not
> from the files listed whitin.
>
> I appreciate your help!
>
>


filenames=`cat fromfile`
egrep -n searching_pattern $filenames
Ed Morton

2006-08-09, 1:26 am

Comcast News wrote:
> [This followup was posted to comp.unix.questions]
>
> While wandering through cyberspace on 8 Aug 2006 17:28:30 -0700, said
> ...
>
>
>
> filenames=`cat fromfile`
> egrep -n searching_pattern $filenames


Won't work if the file names contain spaces. Try this instead:

while IFS= read -n filename
do
awk '/context/ {print NR,$0}' < "$filename"
done < fromfile

Regards,

Ed.
Stephane Chazelas

2006-08-09, 7:27 am

On Tue, 8 Aug 2006 21:08:18 -0400, Comcast News wrote:
[...]
> filenames=`cat fromfile`


if "fromfile" is a newline separated list of file paths, then
you need:

IFS='
'
set -f

In order for the word splitting below to work correctly (and I
suppose you don't want wildcards to be expanded).

> egrep -n searching_pattern $filenames


grep -En searching_pattern -- $filenames

Or simply write "fromfile" in xargs format and run:

xargs grep -En searching_pattern < fromfile


--
Stephane
axel.orduna@gmail.com

2006-08-09, 1:28 pm

Ed
Thanks,

The following lines worked

while IFS= read filename
do
awk '/context/ {print NR,$0}' < "$filename"
done < fromfile


What do I need to add to ignoring case. (ie the string pattern could
be CONTEXT or context or Context)

Do I need to use grep insted of awk in the while loop ?

Bill Marcum

2006-08-09, 1:28 pm

On 9 Aug 2006 09:31:37 -0700, axel.orduna@gmail.com
<axel.orduna@gmail.com> wrote:
> Ed
> Thanks,
>
> The following lines worked
>
> while IFS= read filename
> do
> awk '/context/ {print NR,$0}' < "$filename"
> done < fromfile
>
>
> What do I need to add to ignoring case. (ie the string pattern could
> be CONTEXT or context or Context)
>
> Do I need to use grep insted of awk in the while loop ?
>

You could use
grep -in 'context'
or
awk 'tolower($0)~/context/ {print NR,$0}'


--
<SomeLamer> what's the difference between chattr and chmod?
<SomeGuru> SomeLamer: man chattr > 1; man chmod > 2; diff -u 1 2 | less
-- Seen on #linux on irc
axel.orduna@gmail.com

2006-08-09, 1:28 pm

Bill
Thanks for your help.

I ran:

#!/bin/ksh
while IFS= read -n filename
do
awk 'tolower($0)~/context/ {print NR,$0}' < "$filename"
done < fromfile


But I got the message:

findlist3.sh[2]: read: bad option(s)

Did I miss something (I used nawk, but it don't work either).


Bill Marcum wrote:
> On 9 Aug 2006 09:31:37 -0700, axel.orduna@gmail.com
> <axel.orduna@gmail.com> wrote:
> You could use
> grep -in 'context'
> or
> awk 'tolower($0)~/context/ {print NR,$0}'
>
>
> --
> <SomeLamer> what's the difference between chattr and chmod?
> <SomeGuru> SomeLamer: man chattr > 1; man chmod > 2; diff -u 1 2 | less
> -- Seen on #linux on irc


Chris F.A. Johnson

2006-08-09, 1:28 pm

On 2006-08-09, axel.orduna@gmail.com wrote:
> Bill
> Thanks for your help.
>
> I ran:
>
> #!/bin/ksh
> while IFS= read -n filename
> do
> awk 'tolower($0)~/context/ {print NR,$0}' < "$filename"
> done < fromfile
>
>
> But I got the message:
>
> findlist3.sh[2]: read: bad option(s)
>
> Did I miss something (I used nawk, but it don't work either).


Read the error message.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com