For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2005 > getline with different FS









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 getline with different FS
jujubi

2005-11-08, 6:55 pm

Does anyone know how to specify a different field separator for awk
getline ?

some thing like this..

awk 'BEGIN{FS=" ";}{
yada yada yada..

while..
getline myvar < file2
....
close file2

}' file1

i want to read file1 with FS "<space>" and file2 with FS "-", how to
accomplish that.

Here is what is wrong if the second file has a line like below

abc-ky whlhs sd sags sgsg sagfgf-sdalsg sg s sgs-sag

myvar has abc-ky whlhs sd sags sgsg sagfgf-sdalsg sg s sgs-sag
ie: the spaces are trimed and i dont want that.


Thanks inadvance for all suggestions & comments..

Ed Morton

2005-11-08, 6:55 pm



jujubi wrote:
> Does anyone know how to specify a different field separator for awk
> getline ?


Using getline is almost always the wrong solution.

> some thing like this..
>
> awk 'BEGIN{FS=" ";}{


The above sets FS to it's default setting. You don't need to do that.

> yada yada yada..
>
> while..
> getline myvar < file2


The above is the wrong way to use getline in this context. It should be:

if ((getline myvar < file2) > 0) ...

> ...
> close file2
>
> }' file1
>
> i want to read file1 with FS "<space>" and file2 with FS "-", how to
> accomplish that.


Just set the FS to "-" between the 2 files, e.g.

awk '{print $1}' file1 FS="-" file2

> Here is what is wrong if the second file has a line like below
>
> abc-ky whlhs sd sags sgsg sagfgf-sdalsg sg s sgs-sag
>
> myvar has abc-ky whlhs sd sags sgsg sagfgf-sdalsg sg s sgs-sag
> ie: the spaces are trimed and i dont want that.


No they aren't. Using getline into a variable from a file as you're
doing has no effect on $0 or any positional parameters or any other
built-in variables and the FS setting has no effect.

>
> Thanks inadvance for all suggestions & comments..
>


If you post some sample input and output, you could get more help....

Ed.
jujubi

2005-11-09, 6:55 pm

Ed,

Thanks awk '{print $1}' file1 FS="-" file2 solved my purpose.

[color=darkred]
>No they aren't. Using getline into a variable from a file as you're
>doing has no effect on $0 or any positional parameters or any other
>built-in variables and the FS setting has no effect.


You are right, my mistake.

Sponsored Links







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

Copyright 2008 codecomments.com