For Programmers: Free Programming Magazines  


Home > Archive > AWK > January 2005 > for loops converting spaces to new lines









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 for loops converting spaces to new lines
resonantvox@gmail.com

2005-01-16, 3:55 pm

I'm trying to extract some info from a file and redirect it to grep
doing this:

for x in `cat file | awk -F\" '{print $2}'`; do grep -i $x otherfile;
done

What it's doing is converting the spaces to new lines so:

for x in `cat t4soldspells | awk -F\" '{print $2}'`; do echo $x; done

Yields:

Bob
Smith
Mary
Jane
John
D.
Doe

Instead of how it's formatted if just sent to the terminal

Bob Smith
Mary Jane
John D. Doe
So, the grep is useless.

What am I missing?

Thanks in advance.

Vox

Ed Morton

2005-01-16, 3:55 pm



resonantvox@gmail.com wrote:
> I'm trying to extract some info from a file and redirect it to grep
> doing this:
>
> for x in `cat file | awk -F\" '{print $2}'`; do grep -i $x otherfile;
> done
>
> What it's doing is converting the spaces to new lines so:
>
> for x in `cat t4soldspells | awk -F\" '{print $2}'`; do echo $x; done
>
> Yields:
>
> Bob
> Smith
> Mary
> Jane
> John
> D.
> Doe
>
> Instead of how it's formatted if just sent to the terminal
>
> Bob Smith
> Mary Jane
> John D. Doe
> So, the grep is useless.
>
> What am I missing?


See question 16 in the comp.unix.shell FAQ
(http://home.comcast.net/~j.p.h/cus-faq.html#P) for why your shell
script is misbehaving as written. You also have a UUOC and aren't
quopting properly - follow up in comp.unix.shell if you want to pursue
the shell solution.

Having said that, here's how to do what you want in awk:

Instead of:

for x in `cat t4soldspells | awk -F\" '{print $2}'`; do echo $x; done

do this:

awk -F\" '{print $2}' t4soldspells

and instead of:

for x in `cat file | awk -F\" '{print $2}'`;
do grep -i $x otherfile;
done

do this:

awk -F\" -vIGNORECASE=1 '
NR==FNR{names[$2]="";next}
{
for (name in names) {
if ( $0 ~ name ) {
print
}
}
}' t4soldspells otherfile

IGNORECASE is gawk-specific. Use gawk.

Regards,

Ed.

> Thanks in advance.
>
> Vox
>

resonantvox@gmail.com

2005-01-16, 3:55 pm

Thanks! Works great!

William James

2005-01-16, 3:55 pm


resonantvox@gmail.com wrote:
> for x in `cat file | awk -F\" '{print $2}'`; do grep -i $x otherfile;
> done
>
> What it's doing is converting the spaces to new lines so:
>
> for x in `cat t4soldspells | awk -F\" '{print $2}'`; do echo $x; done
>
> Yields:
>
> Bob
> Smith
> Mary
> Jane
> John
> D.
> Doe
>
> Instead of how it's formatted if just sent to the terminal
>
> Bob Smith
> Mary Jane
> John D. Doe
> So, the grep is useless.



Program hunt.awk:

BEGIN { FS="\"" }
NR==FNR { names[tolower($2)]=1; next }
tolower($0) in names

Run with
awk -f hunt.awk t4soldspells otherfile

Ed Morton

2005-01-16, 3:55 pm



William James wrote:
> resonantvox@gmail.com wrote:
>
>
>
>
> Program hunt.awk:
>
> BEGIN { FS="\"" }
> NR==FNR { names[tolower($2)]=1; next }
> tolower($0) in names


The above assumes that the only text on each line of the second file is
the "$2" from the first file so, unlike the "grep" solution the OP was
trying to use, it'd fail to find "Bob Smith" if the second file
contained a line like "My name is Bob Smith".

Ed.

> Run with
> awk -f hunt.awk t4soldspells otherfile
>

William James

2005-01-16, 3:55 pm

If the first file is small:

BEGIN { FS="\"" }
NR==FNR { re = re pipe tolower($2); pipe="|"; next }
tolower($0) ~ re

resonantvox@gmail.com

2005-01-20, 3:56 pm

Thanks! Works great!

William James

2005-01-20, 3:56 pm

If the first file is small:

BEGIN { FS="\"" }
NR==FNR { re = re pipe tolower($2); pipe="|"; next }
tolower($0) ~ re

Sponsored Links







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

Copyright 2008 codecomments.com