|
Home > Archive > Unix Shell > December 2007 > How do I search a file and return matching filenames?
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 |
How do I search a file and return matching filenames?
|
|
| dawingz@gmail.com 2007-12-18, 7:33 am |
| I would like to parse a file and return a matching filename e.g.
search abc.log and return all occurrences of *.txt. I want the
filenames only not the entire matching line. I haven't been able to
find an example of this using either sed or grep. Any ideas will be
appreciated.
Thanks.
| |
| Loki Harfagr 2007-12-18, 7:33 am |
| Tue, 18 Dec 2007 05:04:48 -0800, dawingz did catÂ_:
> I would like to parse a file and return a matching filename e.g. search
> abc.log and return all occurrences of *.txt. I want the filenames only
> not the entire matching line.
Please define 'filename', if you can define it you will
probably not be very far to write the correct regexp that
will select and trim what you want.
> I haven't been able to find an example of
> this using either sed or grep. Any ideas will be appreciated.
I'd suppose that it'd be easier in awk, feasible in sed
and very depending on your context with grep, now maybe
your def of 'filename' is very strict so that would be
easy in grep as well.
| |
| dawingz@gmail.com 2007-12-18, 1:32 pm |
| On Dec 18, 8:16 am, Loki Harfagr <loki.harf...@asgard.vh> wrote:
> Tue, 18 Dec 2007 05:04:48 -0800, dawingz did cat :
>
>
> Please define 'filename', if you can define it you will
> probably not be very far to write the correct regexp that
> will select and trim what you want.
>
>
> I'd suppose that it'd be easier in awk, feasible in sed
> and very depending on your context with grep, now maybe
> your def of 'filename' is very strict so that would be
> easy in grep as well.
The filenames are alphanumeric, it may contain lowercase and uppercase
letters and numbers, no spaces or other special characters and always
end with ".txt".
e.g. sales100.txt, finance.txt
| |
|
| On 18 Dez., 14:44, dawi...@gmail.com wrote:
> On Dec 18, 8:16 am, Loki Harfagr <loki.harf...@asgard.vh> wrote:
>
>
>
>
>
>
>
>
> The filenames are alphanumeric, it may contain lowercase and uppercase
> letters and numbers, no spaces or other special characters and always
> end with ".txt".
> e.g. sales100.txt, finance.txt
#!/bin/sh
# pass file(s) to parse as argument(s)
awk 'match ($0, /[[:alnum:]]+.txt/) {
print substr ($0, RSTART, RLENGTH)
}' "$@"
If there can be more than one file pattern on a line the program will
have to be slightly extended.
Janis
| |
|
| On 18 Dez., 15:35, Janis <janis_papanag...@hotmail.com> wrote:
> On 18 Dez., 14:44, dawi...@gmail.com wrote:
>
>
>
>
>
>
>
>
>
>
> #!/bin/sh
> # pass file(s) to parse as argument(s)
>
> awk 'match ($0, /[[:alnum:]]+.txt/) {
> print substr ($0, RSTART, RLENGTH)
> }' "$@"
>
> If there can be more than one file pattern on a line the program will
> have to be slightly extended.
I am going to post that extension to take the opportunity fixing a bug
in the above code (missing \ before the .)
awk '
{ for (l = $0; match (l, /[[:alnum:]]+\.txt/);
l = substr (l, RSTART+RLENGTH))
print substr (l, RSTART, RLENGTH)
}
' "$@"
(Ignoring the question how to handle entries like "1.txt2.txt3.txt",
if possible to appear at all)
>
> Janis
| |
| Ed Morton 2007-12-18, 1:32 pm |
|
On 12/18/2007 7:44 AM, dawingz@gmail.com wrote:
> On Dec 18, 8:16 am, Loki Harfagr <loki.harf...@asgard.vh> wrote:
>
>
>
> The filenames are alphanumeric, it may contain lowercase and uppercase
> letters and numbers, no spaces or other special characters and always
> end with ".txt".
> e.g. sales100.txt, finance.txt
If your awk supports REs as RSs (e.g. GNU awk):
gawk -v RS='[^[:alnum:].]+' '/\.txt/' abc.log
Ed.
| |
| dawingz@gmail.com 2007-12-19, 1:23 pm |
| This worked perfectly. Thank You!
Dwight
|
|
|
|
|