getting fields NOT comma delimited with commas inside 
Author Message
 getting fields NOT comma delimited with commas inside

hi all. i'm sorry for my english, but:

i have a trouble creating a shell script that use awk.
i should parse a file with entries like this one:

TEXT`shellcommand`TEXT

for example,

HELLO_`echo "WORLD"`!

should become:

HELLO_WORLD!

this is what i have done:

----file prova.tab
NORM.P_ARBDOC:P07_`echo $CICLO`
NORM.P_ARBDOC:P08_`echo $CICLO`
MAT_NORMA.P_MATASS_`echo $CICLO|awk '{print $0}'`
MAT_NORMA.P_MATASS_`echo $CICLO|awk '{print $0,"1"}'`
----EOF

and the shellscript is:

#!/usr/bin/ksh

export IFS="
"
export CICLO="20021031"

for TABLE in `cat prova.tab | grep -v "^#" | grep -v "^$"` ; do
        echo $TABLE | awk -F '`' '{
                if ( $0 ~ /\`(.*)\`/)   <--- regexp to match a line with ``
                        {
                        print $1,$2,$3  <-- this is a print for debug
                        eval $2 |  getline value
                        printf ("%s%s%s\n\n\n",$1,value,$3)
                        }
                else
                        print $0,"\n\n"
        }'
done

everything works fine except when i want a comma inside $2.
the output is:

-------------STDOUT
NORM.P_ARBDOC:P07_ echo $CICLO
NORM.P_ARBDOC:P07_20021031

NORM.P_ARBDOC:P08_ echo $CICLO
NORM.P_ARBDOC:P08_20021031

MAT_NORMA.P_MATASS_ echo $CICLO|awk '{print $0}'
MAT_NORMA.P_MATASS_20021031

MAT_NORMA.P_MATASS_ echo $CICLO|awk 'print $0'  <--- THIS DOESN'T MATCH
 syntax error The source line is 1.             WHAT I HAVE IN prova.tab!!!
 The error context is
                print >>>   <<<
 awk: Quitting
 The source line is 1.
MAT_NORMA.P_MATASS_

MAT_NORMA.P_MATASS_ echo $CICLO|awk '"1"'
MAT_NORMA.P_MATASS_20021031
-------------EOF

any ideas??
my mail address is valid for replies

many thanks
--
Punkarruso
mail:punkarruso chiocchiolona despammed punto com
pubblicit progresso:
http://www.*-*-*.com/



Tue, 22 Mar 2005 16:54:21 GMT  
 getting fields NOT comma delimited with commas inside

Quote:

> ----file prova.tab
> NORM.P_ARBDOC:P07_`echo $CICLO`
> NORM.P_ARBDOC:P08_`echo $CICLO`
> MAT_NORMA.P_MATASS_`echo $CICLO|awk '{print $0}'`
> MAT_NORMA.P_MATASS_`echo $CICLO|awk '{print $0,"1"}'`
> ----EOF

> and the shellscript is:

> #!/usr/bin/ksh

> export IFS="
> "
> export CICLO="20021031"

> for TABLE in `cat prova.tab | grep -v "^#" | grep -v "^$"` ; do
>         echo $TABLE | awk -F '`' '{
>                 if ( $0 ~ /\`(.*)\`/)   <--- regexp to match a line with
>                 ``
>                         {
>                         print $1,$2,$3  <-- this is a print for debug
>                         eval $2 |  getline value
>                         printf ("%s%s%s\n\n\n",$1,value,$3)
>                         }
>                 else
>                         print $0,"\n\n"
>         }'
> done

i didn't find the solution, but an 'ugly'  workaround...

inside the prova.tab i use ~ for , and inside the script i replace the ~
wit the , before evaluation.

bye
--
Punkarruso
mail:punkarruso chiocchiolona despammed punto com
pubblicit progresso:
http://www.gnu.org/gnu/why-gnu-linux.it.html



Tue, 22 Mar 2005 17:26:00 GMT  
 getting fields NOT comma delimited with commas inside
Quote:

> hi all. i'm sorry for my english, but:

> i have a trouble creating a shell script that use awk.
> i should parse a file with entries like this one:

> TEXT`shellcommand`TEXT

> for example,

> HELLO_`echo "WORLD"`!

> should become:

> HELLO_WORLD!

> this is what i have done:

> ----file prova.tab
> NORM.P_ARBDOC:P07_`echo $CICLO`
> NORM.P_ARBDOC:P08_`echo $CICLO`
> MAT_NORMA.P_MATASS_`echo $CICLO|awk '{print $0}'`
> MAT_NORMA.P_MATASS_`echo $CICLO|awk '{print $0,"1"}'`
> ----EOF

<snip>

Try the following.  In this version, you can have many `command`
occurrences on a single line:

#!/bin/awk -f
{
    if ($0 ~ /^#/ || $0 ~ /^$/) next
    line=$0
    while ((left=index(line,"`")))
    {
        printf("%s",substr(line,1,left-1))
        line=substr(line,left+1)
        right=index(line,"`")
        cmd=substr(line,1,right-1)
        c=0
        while (cmd | getline cmdout)
        {
            if (++c>1) print ""
            printf("%s",cmdout)
        }
        close(cmd)
        printf("%s",cmdout)
        line=substr(line,right+1)
    }
    print line

Quote:
}

Mark.


Wed, 23 Mar 2005 00:27:57 GMT  
 getting fields NOT comma delimited with commas inside

Quote:


> > ----file prova.tab
> > NORM.P_ARBDOC:P07_`echo $CICLO`
> > NORM.P_ARBDOC:P08_`echo $CICLO`
> > MAT_NORMA.P_MATASS_`echo $CICLO|awk '{print $0}'`
> > MAT_NORMA.P_MATASS_`echo $CICLO|awk '{print $0,"1"}'`
> > ----EOF

On the tube home, I realised that the code I posted earlier could have
been written more efficiently.  Here's what I *meant* to write earlier
!

#!/bin/awk -f
BEGIN { FS="`" }
FS>0 && /^[^#]/ {
    for (field=1;field<=NF;field++)
    {
        if (field%2) printf("%s",$(field))
        else {
            line=0
            while ($(field) | getline cmdout)
                printf("%s%s",++line>1?"\n":"",cmdout)

            close(cmdout)
        }
    }
    print ""

Quote:
}

Yep.  That's less braindead.

Mark.



Wed, 23 Mar 2005 01:43:43 GMT  
 getting fields NOT comma delimited with commas inside
quoto quello che dice Mark R.Bannister, perch quotare divertente!!

Quote:
> On the tube home, I realised that the code I posted earlier could have
> been written more efficiently.  Here's what I *meant* to write earlier
> !

> #!/bin/awk -f
> BEGIN { FS="`" }
> FS>0 && /^[^#]/ {
>     for (field=1;field<=NF;field++)
>     {
>         if (field%2) printf("%s",$(field))
>         else {
>             line=0
>             while ($(field) | getline cmdout)
>                 printf("%s%s",++line>1?"\n":"",cmdout)

>             close(cmdout)
>         }
>     }
>     print ""
> }

> Yep.  That's less braindead.

> Mark.

YUP! thank you very much.
i'll enjoy awk, it's a must.

i tried you're code.
the first works fine and it's exactly what i need.
the second it's cool but every 2 lines, the environment variables are lost.
ex:
NORM.P_ARBDOC:P07_`echo $CICLO`
NORM.P_ARBDOC:P08_`echo $CICLO`
MAT_NORMA.P_MATASS_`echo $CICLO|awk '{print $0}'`
MAT_NORMA.P_MATASS_`echo $CICLO|awk '{print $0}'`
cause:
NORM.P_ARBDOC:P07_20021031
NORM.P_ARBDOC:P08_
MAT_NORMA.P_MATASS_20021031
MAT_NORMA.P_MATASS_

BTW, my problem is solved WITHOUT my previous work-around.
many thanks

bye
--
Punkarruso
mail:punkarruso chiocchiolona despammed punto com
pubblicit progresso:
http://www.gnu.org/philosophy/free-sw.it.html



Fri, 25 Mar 2005 14:57:42 GMT  
 getting fields NOT comma delimited with commas inside
Quote:

> YUP! thank you very much.
> i'll enjoy awk, it's a must.

> i tried you're code.
> the first works fine and it's exactly what i need.
> the second it's cool but every 2 lines, the environment variables are lost.

<snip>

Well, just to confuse matters, Google posted my first version (21
lines) *after* my second version (16 lines).  So of course, when you
say the second version didn't work, I assume you actually mean the 21
line version.  Which is fine, because I preferred my leaner 16 line
version anyway.

Glad to have been of help.

Mark.



Sat, 26 Mar 2005 20:28:29 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. FS is a comma, but comma in data!

2. Comma dot (1,000.00) to dot comma (1.000,00)

3. Dealing with commas as a field separator AND possibly within a field

4. Parsing Comma delimited files in J

5. Convert comma-delimited records to fixed length records

6. matching records in a comma delimited file

7. comma delimited

8. Comma delimited file problem

9. AWK & Comma Delimited Text

10. Reading Comma delimited, Quoted String records

11. 2.01 Comma delimited ASCII file

12. Import comma delimited text file

 

 
Powered by phpBB® Forum Software