For Programmers: Free Programming Magazines  


Home > Archive > AWK > January 2008 > match nth field and print the whole result









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 match nth field and print the whole result
aclhkaclhk

2008-01-04, 7:58 am

We have a number of files:

1~peter~hello.txt
2~tom~bye.txt
3~ken~love.txt
4~tom~good.txt

we would like to search for 2nd field containing "tom" and print the
whole filenames.
2~tom~bye.txt
4~tom~good.txt

we would search something like this:
eg. ls . | awk -F"~" '{print $0}'

pls advise what awk construct could give my output.
Janis Papanagnou

2008-01-04, 7:58 am

aclhkaclhk wrote:
> We have a number of files:
>
> 1~peter~hello.txt
> 2~tom~bye.txt
> 3~ken~love.txt
> 4~tom~good.txt
>
> we would like to search for 2nd field containing "tom" and print the
> whole filenames.
> 2~tom~bye.txt
> 4~tom~good.txt
>
> we would search something like this:
> eg. ls . | awk -F"~" '{print $0}'
>
> pls advise what awk construct could give my output.


2nd field containing "tom"...

awk -F"~" '$2~/tom/{print $0}'

or just

awk -F"~" '$2~/tom/'

and 2nd field _is_ "tom"...

awk -F"~" '$2=="tom"'


Janis
pop

2008-01-04, 6:58 pm

aclhkaclhk said the following on 1/4/2008 4:39 AM:
> We have a number of files:
>
> 1~peter~hello.txt
> 2~tom~bye.txt
> 3~ken~love.txt
> 4~tom~good.txt
>
> we would like to search for 2nd field containing "tom" and print the
> whole filenames.
> 2~tom~bye.txt
> 4~tom~good.txt
>
> we would search something like this:
> eg. ls . | awk -F"~" '{print $0}'
>
> pls advise what awk construct could give my output.

simpliest:
contains:
awk '/~.*tom.*~/'
exact match:
awk '/~tom~/'

--
(^\pop/^)
I Stopped to think but forgot to start again.
--
Ed Morton

2008-01-04, 6:58 pm



On 1/4/2008 2:37 PM, pop wrote:
> aclhkaclhk said the following on 1/4/2008 4:39 AM:
>
>
> simpliest:
> contains:
> awk '/~.*tom.*~/'
> exact match:
> awk '/~tom~/'
>


True, assuming exactly 2 "~"s per file name, but then since the OP is apparently
on UNIX, there's an even simpler way of listing just the files with "tom"
between "~"s! He may want to follow up at comp.unix.shell.

Ed.

aclhkaclhk

2008-01-05, 3:58 am

On Jan 4, 8:15 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> aclhkaclhk wrote:
>
>
>
>
>
> 2nd field containing "tom"...
>
> awk -F"~" '$2~/tom/{print $0}'
>
> or just
>
> awk -F"~" '$2~/tom/'
>
> and 2nd field _is_ "tom"...
>
> awk -F"~" '$2=="tom"'
>
> Janis


Thanks all for your kind advise

this one is most suitable for me:
awk -F"~" '$2=="tom"'

I have tested it but this is an exact match. How to match nth field
containing "tom"?

eg.
1~peter~hello.txt
2~tom123~bye.txt
3~ken~love.txt
4~tom456~good.txt

something like: awk -F"~" '$2=="*tom*"' returns
2~tom123~bye.txt
4~tom456~good.txt

Pls advise.
Janis Papanagnou

2008-01-05, 3:58 am

aclhkaclhk wrote:
> On Jan 4, 8:15 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
> wrote:
>
>
>
> Thanks all for your kind advise
>
> this one is most suitable for me:
> awk -F"~" '$2=="tom"'
>
> I have tested it but this is an exact match. How to match nth field
> containing "tom"?


Already answered above...

awk -F"~" '$2~/tom/'


Janis

>
> eg.
> 1~peter~hello.txt
> 2~tom123~bye.txt
> 3~ken~love.txt
> 4~tom456~good.txt
>
> something like: awk -F"~" '$2=="*tom*"' returns
> 2~tom123~bye.txt
> 4~tom456~good.txt
>
> Pls advise.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com