"Hagay Unterman" <[EMAIL PROTECTED]> writes:

> tr " " "\n"

This would be excusable if it were prepended with "UNTESTED". It does
not do what the OP wants.

In general, handling string literals with regexps is not trivial,
because you need to take into account escaped ", as in

"foo \"sna fu\" bar"

and more complicated variants. Also, what if there are newlines inside
the string?

Assuming there are only simple cases in your input (and some
other things like there is no

foo "sna fu"bar

i.e. quoted strings are always whitespace-separated fields) here is a
simple gawk parser that works on your example:

#!/bin/gawk -f

function tail(str,head) { return substr(str,head+1,length(str)-head+1); }

function trprint(str) { gsub(/[ \t]+/,"\n",str); printf("%s",str); }

{
    if (!NF) next;    
    str = $0;
    while (len = index(str,"\"")) {
        trprint(substr(str,1,len-1));
        str = tail(str,len);
        end = index(str,"\"");
        if (!end) {
            printf("%s:%d: unmatched quote at position %d\n",
                   FILENAME,NR,len) > "/dev/stderr";
            exit(1);
        }
        printf("%s\n",substr(str,1,end-1));
        str = tail(str,end+1);
    }
    trprint(str);
    printf("\n");
}


> 
> ------------------------------------------------------------------------------
> 
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
> Behalf Of Tal Achituv
> Sent: Tuesday, February 10, 2004 15:04 PM
> To: [EMAIL PROTECTED]
> Subject: Regexps
> 
> Hi Guys,
> 
> �
> 
> can anyone give me a regular expression that turns
> 
> bla foo bar "kuku 2" test test
> 
> �
> 
> into:
> 
> bla
> 
> foo
> 
> bar
> 
> kuku 2
> 
> test
> 
> test
> 
> �
> 
> (replaces spaces with \n only if its not encapsulated in ")
> 
> �
> 
> Thanks,
> 
> Tal.

Hope it helps,

-- 
Oleg Goldshmidt | [EMAIL PROTECTED]

================================================================To unsubscribe, send 
mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to