problem with spaces in quoted string arguments
problem with spaces in quoted string arguments
am 26.10.2007 03:43:04 von sonnichs
I am passing a list of file records to awk in an attempt to extract
the 2nd field of each record. The records consist of strings seprated
by a space. Some of the strings themselves contain spaces and these
are in double quotes. For example:
10.1.1.2 "testserver" 128.8.183.2
10.1.2.2 "photon hub" 128.8.181.2
10.1.1.7 "voltserver" 128.8.187.4
I simply cat the file into gawk and I was hoping to extract the name
fields:
cat test2 | awk '{print $2}'
"testserver"
"photon
"voltserver"
As can be seen in the output, the server called "photon hub" did not
extract properly, since the space was detected in the argument to awk.
I have looked for an option to ignore spaces within double quotes and
can find none. The easy thing to do is write a quick C program to do
this, but perhaps there is a simple work around that I do not know
about.
Thanks
Fritz
Re: problem with spaces in quoted string arguments
am 26.10.2007 03:53:07 von Janis Papanagnou
rec.woodworking wrote:
> I am passing a list of file records to awk in an attempt to extract
> the 2nd field of each record. The records consist of strings seprated
> by a space. Some of the strings themselves contain spaces and these
> are in double quotes. For example:
>
> 10.1.1.2 "testserver" 128.8.183.2
> 10.1.2.2 "photon hub" 128.8.181.2
> 10.1.1.7 "voltserver" 128.8.187.4
>
> I simply cat the file into gawk and I was hoping to extract the name
> fields:
>
> cat test2 | awk '{print $2}'
> "testserver"
> "photon
> "voltserver"
>
> As can be seen in the output, the server called "photon hub" did not
> extract properly, since the space was detected in the argument to awk.
> I have looked for an option to ignore spaces within double quotes and
> can find none. The easy thing to do is write a quick C program to do
> this, but perhaps there is a simple work around that I do not know
> about.
If your data is as shown above and you just want to extract the one
field then you can redefine the field separator...
awk -F\" '{print $2}' test2
Janis
>
> Thanks
> Fritz
>
Re: problem with spaces in quoted string arguments
am 26.10.2007 03:53:44 von cfajohnson
On 2007-10-26, rec.woodworking wrote:
>
>
> I am passing a list of file records to awk in an attempt to extract
> the 2nd field of each record. The records consist of strings seprated
> by a space. Some of the strings themselves contain spaces and these
> are in double quotes. For example:
>
> 10.1.1.2 "testserver" 128.8.183.2
> 10.1.2.2 "photon hub" 128.8.181.2
> 10.1.1.7 "voltserver" 128.8.187.4
>
> I simply cat the file into gawk and I was hoping to extract the name
> fields:
>
> cat test2 | awk '{print $2}'
> "testserver"
> "photon
> "voltserver"
>
> As can be seen in the output, the server called "photon hub" did not
> extract properly, since the space was detected in the argument to awk.
> I have looked for an option to ignore spaces within double quotes and
> can find none. The easy thing to do is write a quick C program to do
> this, but perhaps there is a simple work around that I do not know
> about.
awk -F\" '{print $2}' test2
Or:
awk '{print $2}' test2 | tr -d '"'
--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Re: problem with spaces in quoted string arguments
am 26.10.2007 03:54:49 von Janis Papanagnou
Janis Papanagnou wrote:
> rec.woodworking wrote:
>
>> I am passing a list of file records to awk in an attempt to extract
>> the 2nd field of each record. The records consist of strings seprated
>> by a space. Some of the strings themselves contain spaces and these
>> are in double quotes. For example:
>>
>> 10.1.1.2 "testserver" 128.8.183.2
>> 10.1.2.2 "photon hub" 128.8.181.2
>> 10.1.1.7 "voltserver" 128.8.187.4
>>
>> I simply cat the file into gawk and I was hoping to extract the name
>> fields:
>>
>> cat test2 | awk '{print $2}'
>> "testserver"
>> "photon
>> "voltserver"
>>
>> As can be seen in the output, the server called "photon hub" did not
>> extract properly, since the space was detected in the argument to awk.
>> I have looked for an option to ignore spaces within double quotes and
>> can find none. The easy thing to do is write a quick C program to do
>> this, but perhaps there is a simple work around that I do not know
>> about.
>
>
> If your data is as shown above and you just want to extract the one
> field then you can redefine the field separator...
>
> awk -F\" '{print $2}' test2
awk -F\" '{print "\"" $2 "\""}'
....if you want to keep the quotes.
>
>
> Janis
>
>>
>> Thanks
>> Fritz
>>
Re: problem with spaces in quoted string arguments
am 26.10.2007 04:13:10 von cfajohnson
On 2007-10-26, Chris F.A. Johnson wrote:
> On 2007-10-26, rec.woodworking wrote:
>> I am passing a list of file records to awk in an attempt to extract
>> the 2nd field of each record. The records consist of strings seprated
>> by a space. Some of the strings themselves contain spaces and these
>> are in double quotes. For example:
>>
>> 10.1.1.2 "testserver" 128.8.183.2
>> 10.1.2.2 "photon hub" 128.8.181.2
>> 10.1.1.7 "voltserver" 128.8.187.4
>>
>> I simply cat the file into gawk and I was hoping to extract the name
>> fields:
>>
>> cat test2 | awk '{print $2}'
>> "testserver"
>> "photon
>> "voltserver"
>>
>> As can be seen in the output, the server called "photon hub" did not
>> extract properly, since the space was detected in the argument to awk.
>> I have looked for an option to ignore spaces within double quotes and
>> can find none. The easy thing to do is write a quick C program to do
>> this, but perhaps there is a simple work around that I do not know
>> about.
>
> awk -F\" '{print $2}' test2
>
> Or:
Instead of
> awk '{print $2}' test2 | tr -d '"'
what I mean to write was:
awk '{ gsub( /^[^"]*/, "" )
gsub( /[^"]*$/, "" )
print }' test2 | tr -d '"'
--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Re: problem with spaces in quoted string arguments
am 26.10.2007 15:31:12 von sonnichs
OK and THANKS!
You guys have been a lot of help and these methods pushed me past my
problem
Thanks
Fritz