simple formatting...but not so easy 
Author Message
 simple formatting...but not so easy

Hello.

I'm quite new with awk and I almost managed to do what I wanted but I
still have a problem.
in lot of files Ihave this line : NAME = Hello world !
and I want to transform it to : NAME = "Hello world !"

so I built this rule :
$1 ~ /^\ *NAME.*/  {
  print $1 " " $2 " \""$3"\""
    next;

Quote:
}

but the output I got is : NAME = "Hello"
How do I have to do to get all the rest of the line between the " ??

Thanks.

    Laurent.



Tue, 31 Dec 2002 03:00:00 GMT  
 simple formatting...but not so easy

Quote:

> Hello.

> I'm quite new with awk and I almost managed to do what I wanted but I
> still have a problem.
> in lot of files Ihave this line : NAME = Hello world !
> and I want to transform it to : NAME = "Hello world !"

> so I built this rule :
> $1 ~ /^\ *NAME.*/  {
>   print $1 " " $2 " \""$3"\""
>     next;
> }

> but the output I got is : NAME = "Hello"
> How do I have to do to get all the rest of the line between the " ??

Using your method above, you'd need:

 $1 ~ /^\ *NAME.*/  {
   print $1 " " $2 " \""$3, $4, $5"\""
     next;
 }

Because $4 and $5 are world and !.

Tristan.



Tue, 31 Dec 2002 03:00:00 GMT  
 simple formatting...but not so easy
You might try:

BEGIN {
    FS = " = ";

Quote:
}

{
    print $1, "=", "\""$2"\"";

Quote:
}

Setting the field separator to "=" automagically chops up the line into
what comes before the "=" and what comes after (so you get everything
after the "=" as one piece).

If you don't want to have FS be "=" for the whole file, you might try
using split() on the NAME lines with " = " as the split RE.

Aloha,
Brad

Quote:

> Hello.

> I'm quite new with awk and I almost managed to do what I wanted but I
> still have a problem.
> in lot of files Ihave this line : NAME = Hello world !
> and I want to transform it to : NAME = "Hello world !"

> so I built this rule :
> $1 ~ /^\ *NAME.*/  {
>   print $1 " " $2 " \""$3"\""
>     next;
> }

> but the output I got is : NAME = "Hello"
> How do I have to do to get all the rest of the line between the " ??

> Thanks.

>     Laurent.



Tue, 31 Dec 2002 03:00:00 GMT  
 simple formatting...but not so easy

Quote:

> Hello.

> I'm quite new with awk and I almost managed to do what I wanted but I
> still have a problem.
> in lot of files Ihave this line : NAME = Hello world !
> and I want to transform it to : NAME = "Hello world !"

> so I built this rule :
> $1 ~ /^\ *NAME.*/  {
>   print $1 " " $2 " \""$3"\""
>     next;
> }

> but the output I got is : NAME = "Hello"
> How do I have to do to get all the rest of the line between the " ??

> Thanks.

>     Laurent.

Laurent,

You are getting exactly what you are asking for:

$1 = NAME
$2 = =
$3 = Hello

You are not telling awk to do anything with the rest of the line (ie $4
= world and $5 = !).

One way you could handle this would be to change your field seperator to
" = ".  As in:

$ echo NAME = Hello world ! | awk -F" = " '$1 ~ /^\ *NAME.*/  { print $1
" = \""$2"\""  }'

NAME = "Hello world !"

That way $1 = NAME and $2 = Hello world !

Or you could do it using sed:

sed 's/\(^ *NAME.* = \)\(.*\)/\1"\2"/'

<disclaimer>
I don't claim to be an expert at either of these functions so there are
probably better ways of accomplishing the task.  But they do work.
</disclaimer>

Hope it helps.

--
Cheers,
Ted J. Loeffelholz
Senior Design Engineer
Caterpillar Inc



Tue, 31 Dec 2002 03:00:00 GMT  
 simple formatting...but not so easy

Quote:

> Hello.

> I'm quite new with awk and I almost managed to do what I wanted but I
> still have a problem.
> in lot of files Ihave this line : NAME = Hello world !
> and I want to transform it to : NAME = "Hello world !"

> so I built this rule :
> $1 ~ /^\ *NAME.*/  {
>   print $1 " " $2 " \""$3"\""
>     next;
> }

> but the output I got is : NAME = "Hello"
> How do I have to do to get all the rest of the line between the " ??

> Thanks.

>     Laurent.

Ok thank you for all yours answers but it's not so simple.
The example I gave was just an example and quite easy.
the right side of = is not always the same text, it might be from 1 up
to 36 words and more but always on the same line, and it can contains
the sign = (that's why I think I can't change the FS).

imagine this for another complex example :
NAME = This line is right even with this sign : =    !!!
will become (if it's possible)  NAME = "This line is right even with
this sign : =    !!!"

Thanks for all your previous answers...and the next ones.

    Laurent.



Tue, 31 Dec 2002 03:00:00 GMT  
 simple formatting...but not so easy

Quote:
> Ok thank you for all yours answers but it's not so simple.
> The example I gave was just an example and quite easy.
> the right side of = is not always the same text, it might be from 1 up
> to 36 words and more but always on the same line, and it can contains
> the sign = (that's why I think I can't change the FS).

> imagine this for another complex example :
> NAME = This line is right even with this sign : =    !!!
> will become (if it's possible)  NAME = "This line is right even with
> this sign : =    !!!"

> Thanks for all your previous answers...and the next ones.

>     Laurent.

OK then, this is what I was going to post originally:

$1~NAME{sub(/= /,"&\"");printf("%s\"\n",$0)}

This puts a " after the first = and then another
at the end of the line:

$cat test.dat
NAME = Hello world !
NAME = This line is right even with this sign : =    !!!
$ gawk '$1~NAME{sub(/= /,"&\"");printf("%s\"\n",$0)}' test.dat
NAME = "Hello world !"
NAME = "This line is right even with this sign : =    !!!"

Sorry for not having posted this earlier: I was being
lazy and thought you'd appreciate my first answer better
as it was derived straight from your original script.

Tristan.



Tue, 31 Dec 2002 03:00:00 GMT  
 simple formatting...but not so easy

Quote:

> Sorry for not having posted this earlier: I was being
> lazy and thought you'd appreciate my first answer better
> as it was derived straight from your original script.

Ok this works perfectly.
Thanks for all.

    Laurent



Tue, 31 Dec 2002 03:00:00 GMT  
 simple formatting...but not so easy

Quote:


> > Sorry for not having posted this earlier: I was being
> > lazy and thought you'd appreciate my first answer better
> > as it was derived straight from your original script.

> Ok this works perfectly.

It only "works" by accident for the specific case of the limited
example data used to test it.

NAME is a variable, not a regular expression pattern, and is
autoinitialized to the null string. It's value will successfully
match anything to which it is compared using the pattern match
operator (~). Add this line to your test input file:

    NOT_A_NAME = This text should not be quoted.

For such a trivial text substitution task, use sed, which supports
parenthesized subexpression memory, not awk, which does not.

See URL <http://www.userfriendly.org/cartoons/archives/99feb/uf000342.gif>.

--
Jim Monty

Tempe, Arizona USA



Tue, 31 Dec 2002 03:00:00 GMT  
 simple formatting...but not so easy

Quote:

> > Ok thank you for all yours answers but it's not so simple.
> > The example I gave was just an example and quite easy.
> > the right side of = is not always the same text, it might be from 1 up
> > to 36 words and more but always on the same line, and it can contains
> > the sign = (that's why I think I can't change the FS).

> > imagine this for another complex example :
> > NAME = This line is right even with this sign : =    !!!
> > will become (if it's possible)  NAME = "This line is right even with
> > this sign : =    !!!"

> > Thanks for all your previous answers...and the next ones.

> >     Laurent.

> OK then, this is what I was going to post originally:

> $1~NAME{sub(/= /,"&\"");printf("%s\"\n",$0)}

> This puts a " after the first = and then another
> at the end of the line:

> $cat test.dat
> NAME = Hello world !
> NAME = This line is right even with this sign : =    !!!
> $ gawk '$1~NAME{sub(/= /,"&\"");printf("%s\"\n",$0)}' test.dat
> NAME = "Hello world !"
> NAME = "This line is right even with this sign : =    !!!"

> Sorry for not having posted this earlier: I was being
> lazy and thought you'd appreciate my first answer better
> as it was derived straight from your original script.

> Tristan.

The sed solution will still work:

$ cat test.dat
NAME = Hello world !
NAME = This line is right even with this sign : =    !!!
$ sed 's/\(^NAME = \)\(.*\)/\1"\2"/' test.dat
NAME = "Hello world !"
NAME = "This line is right even with this sign : =    !!!"

Didn't know if HAD to be in awk.

--
Cheers,
Ted J. Loeffelholz
Senior Design Engineer
Caterpillar Inc



Tue, 31 Dec 2002 03:00:00 GMT  
 simple formatting...but not so easy
On Fri, 14 Jul 2000 14:01:34 +0200, Laurent Vaills

Quote:

>Hello.

>I'm quite new with awk and I almost managed to do what I wanted but I
>still have a problem.
>in lot of files Ihave this line : NAME = Hello world !
>and I want to transform it to : NAME = "Hello world !"

>so I built this rule :
>$1 ~ /^\ *NAME.*/  {
>  print $1 " " $2 " \""$3"\""
>    next;
>}

>but the output I got is : NAME = "Hello"
>How do I have to do to get all the rest of the line between the " ??

>Thanks.

>    Laurent.

/^ *NAME/ && NF > 2 {
        $3 = "\"" $3
        $NF = $NF "\""
        print $0 # reconstituted with new $3 and $NF included
        next # skip remaining rules

Quote:
}

Thanks. Take care, Brian Inglis         Calgary, Alberta, Canada
--

                                use address above to reply


Fri, 03 Jan 2003 03:00:00 GMT  
 simple formatting...but not so easy


Quote:
>On Fri, 14 Jul 2000 14:01:34 +0200, Laurent Vaills

>>Hello.

>>I'm quite new with awk and I almost managed to do what I wanted but I
>>still have a problem.
>>in lot of files Ihave this line : NAME = Hello world !
>>and I want to transform it to : NAME = "Hello world !"

>>so I built this rule :
>>$1 ~ /^\ *NAME.*/  {
>>  print $1 " " $2 " \""$3"\""
>>    next;
>>}

>>but the output I got is : NAME = "Hello"
>>How do I have to do to get all the rest of the line between the " ??

>>Thanks.

>>    Laurent.

>/^ *NAME/ && NF > 2 {
>    $3 = "\"" $3
>    $NF = $NF "\""
>    print $0 # reconstituted with new $3 and $NF included
>    next # skip remaining rules
>}

Unfortunately, this will reformat the line, compressing multiple
consecutive spaces (actually any whitespace) into a single soace.

To avoid that one could do it this way (there are other ways):

/^ *NAME/ {sub(/= /,"= \"");sub(/$/,"\""); print; next}

By not redefining individual fields, and only using $0 the line is
not reparsed by awk.  This is important if spacing of fields is
important.

One could build more error checking into this if one wished, and
depending on what you know about the data, that might be necessary.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sat, 04 Jan 2003 03:00:00 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Simple task-not simple for Dummy, Help

2. Windows passes filename in 8.3 format and not in long format to tcl script

3. Easiest way to do simple animation (no GUI)

4. A SIMPLE, EASY OBJECT ORIENTED DESIGN DRAWING TOOL

5. Easy Question re spreadsheet format

6. Easiest Graphic File format to display

7. Easy formatting questions :-)

8. easy plural formatting question

9. Shell vars into Awk executable script not so easy

10. Easy but not

 

 
Powered by phpBB® Forum Software