A problem with " 
Author Message
 A problem with "

Hi all,

how  can I get this simple line...


"\""; print $2}'

to print


instead of


?
Do you think there is a way ? I'm a newbie and I just can't came out of
it. I'm sure I'm missing something.
Any help is appreciated, thanks a lot indeed.
Lorso



Mon, 06 Jan 2003 03:00:00 GMT  
 A problem with "

% how  can I get this simple line...
%

% "\""; print $2}'
%
% to print
%

%
% instead of
%

You can't. Your first problem is that you set FS too late. By the time it's
set there, the input record is already divided into fields. You could set
FS using the -F switch:
 awk -F'"' '{ print $2 }'
or in a BEGIN section
 awk 'BEGIN { FS = "\""} { print $2 }'
Given your input, this will print
 hi all

Except in the special case when FS is " ", awk doesn't ignore leading
field separators, so $1 will always be the stuff to the left of the
first double quote, $2 will be the stuff between the first set of double
quotes, $3 will be the space following the second double quote, and $4
will be the text you want. If that's OK, then use it:

 echo '"hi all" "help"' | awk -F'"' '{ print $4 }'

You can get rid of some of the spurious fields by adding optional spaces
into FS
 awk -F' *"[ "]*' '{ print $3 }'
but this might not do exactly what you want, either.
--

Patrick TJ McPhee
East York  Canada



Mon, 06 Jan 2003 03:00:00 GMT  
 A problem with "

Quote:

>Hi all,

>how  can I get this simple line...


>"\""; print $2}'

>to print


>instead of



First of all, "help" is going to $4, not $2.  The string you are passing
parses as follows (with " as the FS):

        $1) <null>
        $2) hi all
        $3) <space>
        $4) help
        $5) <null>

Second, I noticed a difference between "awk on Solaris" and gawk (which is
"awk on Linux").  Observe:

% echo '"hi all" "help"' | awk '{FS = "\""; print $2}'
hi all

% echo '"hi all" "help"' | gawk '{FS = "\""; print $2}'
all"

% echo '"hi all" "help"' | gawk '-F"' '{print $2}'
hi all

I leave it to you to figure out what it all means...



Mon, 06 Jan 2003 03:00:00 GMT  
 A problem with "

   >Hi all,
   >how  can I get this simple line...

   >"\""; print $2}'
   >to print

   >instead of

   >?
   >Do you think there is a way ? I'm a newbie and I just can't came
   >out of it. I'm sure I'm missing something.
Either set FS before awk begins:
echo '"hi all" "help"' | awk -F\" '{ print $2}'
or modify $0 after setting FS:
echo '"hi all" "help"' | awk '{FS ="\""; $0=$0 ""; print $2}'
Note: in your example, if FS=\", "help" will be $4.  (cash, check or Visa :)

Net-Tamer V 1.08X - Test Drive



Mon, 06 Jan 2003 03:00:00 GMT  
 A problem with "

Quote:



> (cash, check or Visa :)

No joke, your comments all are millions of $ worthy (I mean millions of
shell variables... ;).

Thanks so much to all of you.
Lorso



Tue, 07 Jan 2003 03:00:00 GMT  
 A problem with "

Quote:

> You can't. Your first problem is that you set FS too late. By the time it's
> set there, the input record is already divided into fields. You could set
> FS using the -F switch:
>  awk -F'"' '{ print $2 }'
> or in a BEGIN section
>  awk 'BEGIN { FS = "\""} { print $2 }'
> Given your input, this will print
>  hi all

According to A. Robbins in "Effective AWK Programming," the
behavior you've just described is what's supposed to happen
if the version of awk you're using complies with the POSIX
standard.  But in  some non-POSIX compliant awk's, records don't
get parsed into fields until the code is obliged to access
fields and in these cases, a modified FS can conceivably
apply to the current record.  In any event, that's what seems
to happen when I use awk on a SUNOS workstation.   I rarely
run into situations where I'm obliged to change FS, and until
recently, I wasn't even aware that this behavior could be regarded
as a "bug."

record.



Sun, 12 Jan 2003 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Problem about "comp.lang.basic.realbasic"…

2. Problem with "radio" button

3. Problem: Screen "dropouts" in wiring diagram

4. Problem with "SELECT"

5. Problem with "SELECT"

6. Problem with "Noting-Progress" macro

7. Problems with "make.dat"

8. Problem with "null" commands

9. Problem with "Daemon not connected"

10. problem with "Execp"

11. Problems with "Essentials of Programming ..."

12. Problem with "-r" command line argument

 

 
Powered by phpBB® Forum Software