Getting awk (or sed or anything else) to put single quotes into file 
Author Message
 Getting awk (or sed or anything else) to put single quotes into file

I'd like to have awk (or anything else for that matter) process a file
and output
part of a line with single quotes around it.

For example, the input looks like this:

TEXT   ABC
(other data omitted)
TEXT   ABC  123
(other data omitted)
TEXT   XYZ/789 12 A

and I'd like the output to look like this:

TEXT   'ABC'
(other data omitted)
TEXT   'ABC  123'
(other data omitted)
TEXT   'XYZ/789 12 A'

As you can see each line I want to change starts with "TEXT", and I
don't know how
many fields the line may have. I want to surround everything AFTER the
word "TEXT"
by single quotes (').

The simple awk script below will produce the output I want without the
single quotes.
How can I modify the print (or printf) statement to output single quotes
around my
character string called "name"???

awk '{
 if ( $1 == "TEXT" ) {
  name = substr($0,6)
  print $1,   name
     }
 else {
  print $0
     }
 }'  input_file > output_file

Thanks for any help.
Regards,

Kevin Outman



Sat, 20 Jan 2001 03:00:00 GMT  
 Getting awk (or sed or anything else) to put single quotes into file


Quote:
> I'd like to have awk (or anything else for that matter) process a file
> and output
> part of a line with single quotes around it.
...
> The simple awk script below will produce the output I want without the
> single quotes.
> How can I modify the print (or printf) statement to output single quotes
> around my
> character string called "name"???

> awk '{
>  if ( $1 == "TEXT" ) {
>   name = substr($0,6)
>   print $1,   name
>      }
>  else {
>   print $0
>      }
>  }'  input_file > output_file

Instead of "print $1, name", try this:

     printf "%s \047%s\047\n", $1, name

\047 is the octal number of 39 which is the ASCII encoding of the
apostrophe character.

Le /\/ETtoyeur

"Le sexe est une facult qui rtrcit...mme avec Viagra"



Sat, 20 Jan 2001 03:00:00 GMT  
 Getting awk (or sed or anything else) to put single quotes into file
On Tue, 04 Aug 1998 07:38:14 +0800, Kevin Outman

Quote:

>For example, the input looks like this:

>TEXT   ABC
>(other data omitted)
>TEXT   ABC  123
>(other data omitted)
>TEXT   XYZ/789 12 A

>and I'd like the output to look like this:

>TEXT   'ABC'
>(other data omitted)
>TEXT   'ABC  123'
>(other data omitted)
>TEXT   'XYZ/789 12 A'

man sed

sed "s/^TEXT \(.*\)/TEXT '\1'/" file

Untested, you may need to escape(backslash) the backslashes.

Hope that helps,
Douglas Wilson



Sat, 20 Jan 2001 03:00:00 GMT  
 Getting awk (or sed or anything else) to put single quotes into file


Quote:
>I'd like to have awk (or anything else for that matter) process a file
>and output
>part of a line with single quotes around it.

>For example, the input looks like this:

>TEXT   ABC
>(other data omitted)
>TEXT   ABC  123
>(other data omitted)
>TEXT   XYZ/789 12 A

>and I'd like the output to look like this:

>TEXT   'ABC'
>(other data omitted)
>TEXT   'ABC  123'
>(other data omitted)
>TEXT   'XYZ/789 12 A'

sed -f temp.sed infile

where temp.sed is :

# start of sed script
s/^TEXT */&'/
s/^TEXT.*/&'/
# end of sed script

Chuck Demas
Needham, Mass.

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

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



Sat, 20 Jan 2001 03:00:00 GMT  
 Getting awk (or sed or anything else) to put single quotes into file
Kevin,

Using your data, your substr() needs to use 8 instead of 6 to get the
start of the "other" data on a TEXT line (you have three spaces after
the TEXT literal, not one), but the single quotes are simple.  Try
this:

{
    if ( $1 == "TEXT" ) {
        name = substr($0,8)   # note 8 instead of 6
        printf "%s   '%s'\n", $1, name  # note extra spaces before '
    }
    else {
        print $0
    }

Quote:
}

IOW, just put the single quotes inside double quotes.  Using your
script, all you'd need to do is this:

print $1, "   '" name "'"

Remember that spaces between the comma-separated arguments to "print"
are NOT sent to output.  "print $1,   name" is exactly the same as
"print $1, name" or "print $1,            name".  Spaces only count
inside the format string to printf.

HTH.

----------------------------------------------------




Sun, 21 Jan 2001 03:00:00 GMT  
 Getting awk (or sed or anything else) to put single quotes into file


Quote:
> Using your data, your substr() needs to use 8 instead of 6 to get the
> start of the "other" data on a TEXT line (you have three spaces after
> the TEXT literal, not one), but the single quotes are simple.  Try
> this:

> {
>     if ( $1 == "TEXT" ) {
>         name = substr($0,8)   # note 8 instead of 6
>         printf "%s   '%s'\n", $1, name  # note extra spaces before '

Have you tried that code? With my version of awk (AIX 4.1.5),
the quote won't print inside a printf format.

Quote:
> IOW, just put the single quotes inside double quotes.  Using your
> script, all you'd need to do is this:

> print $1, "   '" name "'"

Have you tried that code too? It prints out the word 'name' (without
quotes), not the value of the variable 'name'.

Le /\/ETtoyeur

"Le sexe est une facult qui rtrcit...mme avec Viagra"



Sun, 21 Jan 2001 03:00:00 GMT  
 Getting awk (or sed or anything else) to put single quotes into file

Quote:

>I'd like to have awk (or anything else for that matter) process a file
>and output
>part of a line with single quotes around it.

>For example, the input looks like this:

>TEXT   ABC
>(other data omitted)
>TEXT   ABC  123
>(other data omitted)
>TEXT   XYZ/789 12 A

>and I'd like the output to look like this:

>TEXT   'ABC'
>(other data omitted)
>TEXT   'ABC  123'
>(other data omitted)
>TEXT   'XYZ/789 12 A'

>As you can see each line I want to change starts with "TEXT", and I
>don't know how
>many fields the line may have. I want to surround everything AFTER the
>word "TEXT"
>by single quotes (').

>The simple awk script below will produce the output I want without the
>single quotes.
>How can I modify the print (or printf) statement to output single quotes
>around my
>character string called "name"???

>awk '{
> if ( $1 == "TEXT" ) {
>  name = substr($0,6)
>  print $1,   name
>     }
> else {
>  print $0
>     }
> }'  input_file > output_file

Try something like....
awk '{
 if ( $1 == "TEXT" ) {
#
#
  name = substr($0,7,length($0)-1)
# increase start by one, and do not get the whole line
#
  print $1 "\'" name "\'"
     }
 else {
  print $0
     }
 }'  input_file > output_file

Quote:

>Thanks for any help.
>Regards,

>Kevin Outman

--- Mike Kinney

using a friends computer...      :-)


Sun, 21 Jan 2001 03:00:00 GMT  
 Getting awk (or sed or anything else) to put single quotes into file
[Posted and mailed]

Quote:



>> Using your data, your substr() needs to use 8 instead of 6 to get the
>> start of the "other" data on a TEXT line (you have three spaces after
>> the TEXT literal, not one), but the single quotes are simple.  Try
>> this:

>> {
>>     if ( $1 == "TEXT" ) {
>>         name = substr($0,8)   # note 8 instead of 6
>>         printf "%s   '%s'\n", $1, name  # note extra spaces before '

>Have you tried that code? With my version of awk (AIX 4.1.5),
>the quote won't print inside a printf format.

My gawk v3.0.3 (DOS/DJGPP) processes it just fine, prints the single
quotes no problem.

Quote:
>> IOW, just put the single quotes inside double quotes.  Using your
>> script, all you'd need to do is this:

>> print $1, "   '" name "'"

>Have you tried that code too? It prints out the word 'name' (without
>quotes), not the value of the variable 'name'.

Again, gawk v3.0.3 (DOS/DJGPP) has no problems with this code, it
works the same as the printf statement quoted above.

I should probably have added my version and release of gawk as part of
my reply, sorry for the omission.

A thought just occurred to me -- did you try my code as a script file,
or as a command-line quoted program?  I ran my tests as script files,
*not* from the command-line.  I could see that there might be problems
running my version from the command-line, but not as script files.

HTH.

----------------------------------------------------




Mon, 22 Jan 2001 03:00:00 GMT  
 Getting awk (or sed or anything else) to put single quotes into file


Quote:

> My gawk v3.0.3 (DOS/DJGPP) processes it just fine, prints the single
> quotes no problem.

> Again, gawk v3.0.3 (DOS/DJGPP) has no problems with this code, it
> works the same as the printf statement quoted above.

> I should probably have added my version and release of gawk as part of
> my reply, sorry for the omission.

> A thought just occurred to me -- did you try my code as a script file,
> or as a command-line quoted program?  I ran my tests as script files,
> *not* from the command-line.  I could see that there might be problems
> running my version from the command-line, but not as script files.

I've tried your code inside a script file.
With the original awk (not the GNU version) in AIX, the quote seems act
like a double quote. You can't even print quote with \'
I've got to use \047 to print it. Maybe that's why the original poster
has asked how to do it.

Le /\/ETtoyeur

"find: 0652-019 The status on God is not valid."



Mon, 22 Jan 2001 03:00:00 GMT  
 Getting awk (or sed or anything else) to put single quotes into file

% Have you tried that code? With my version of awk (AIX 4.1.5),
% the quote won't print inside a printf format.

Sigh... this is a common misconception. The easiest way to write an awk
script which is in any way complicated is to put the script in a file
and include the file using the -f option. If you don't do this, you
should keep in mind that the shell is there, doing something to your
input, and you have to understand what the shell is doing. For instance,
this will work, on any machine:

<contents of file x.awk:>
 $1 == "TEXT"  {
         name = substr($0,8)   # note 8 instead of 6
        printf "%s   '%s'\n", $1, name  # note extra spaces before '
 }

<thing to put on command line:>
 awk -f x.awk inputfile

On the other hand, this will not work, on any machine:
 awk '
  $1 == "TEXT"  {
          name = substr($0,8)
         printf "%s   '%s'\n", $1, name
  }
 ' inputfile

The reason it won't work is that you're using single quotes to delimit
the argument to awk. It's not that awk won't print the single-quotes,
it's that the shell sucks them up so awk never sees them. What awk
ends up seeing once the shell is done its bit is this:

  $1 == "TEXT"  {
          name = substr($0,8)
         printf "%s   %s\n", $1, name
  }

The only time you should put an awk program on the command-line
is when it's a one-liner, or under some extreme circumstances when
awk is used as part of a pipe-line in a shell script, but you shouldn't
be writing shell scripts if you don't understand how quoting works.

--

Patrick TJ McPhee
East York  Canada



Mon, 22 Jan 2001 03:00:00 GMT  
 Getting awk (or sed or anything else) to put single quotes into file

% Have you tried that code? With my version of awk (AIX 4.1.5),
% the quote won't print inside a printf format.

Sigh... this is a common misconception. The easiest way to write an awk
script which is in any way complicated is to put the script in a file
and include the file using the -f option. If you don't do this, you
should keep in mind that the shell is there, doing something to your
input, and you have to understand what the shell is doing. For instance,
this will work, on any machine:

<contents of file x.awk:>
 $1 == "TEXT"  {
         name = substr($0,8)   # note 8 instead of 6
        printf "%s   '%s'\n", $1, name  # note extra spaces before '
 }

<thing to put on command line:>
 awk -f x.awk inputfile

On the other hand, this will not work, on any machine:
 awk '
  $1 == "TEXT"  {
          name = substr($0,8)
         printf "%s   '%s'\n", $1, name
  }
 ' inputfile

The reason it won't work is that you're using single quotes to delimit
the argument to awk. It's not that awk won't print the single-quotes,
it's that the shell sucks them up so awk never sees them. What awk
ends up seeing once the shell is done its bit is this:

  $1 == "TEXT"  {
          name = substr($0,8)
         printf "%s   %s\n", $1, name
  }

The only time you should put an awk program on the command-line
is when it's a one-liner, or under some extreme circumstances when
awk is used as part of a pipe-line in a shell script, but you shouldn't
be writing shell scripts if you don't understand how quoting works.

--

Patrick TJ McPhee
East York  Canada



Mon, 22 Jan 2001 03:00:00 GMT  
 Getting awk (or sed or anything else) to put single quotes into file
I don't have the original article, so I'm making an assumption
that the problem is getting an awk print or printf to include
a literal single quote in its output.


|> Kevin,
|>
|> Using your data, your substr() needs to use 8 instead of 6 to get the
|> start of the "other" data on a TEXT line (you have three spaces after
|> the TEXT literal, not one), but the single quotes are simple.  Try
|> this:
|>
|> {
|>     if ( $1 == "TEXT" ) {
|>         name = substr($0,8)   # note 8 instead of 6
|>         printf "%s   '%s'\n", $1, name  # note extra spaces before '
|>     }
|>     else {
|>         print $0
|>     }
|> }

This would not work in a typical awk script where the program
is given as a single quoted argument on the command line.

  awk '
    ...
    print "abc 'foobar' def"
    ...
  ' datafile

The 4 single quotes would be grouped by the shell into 2 pairs
and awk would never see them.

It would work if the program were put in a file and awk was
fun as "awk -f progfile datafile

To do it in a comand line script consider the following:

    '\''

The first quote pairs with the current shell single quote,
the "\'" go to awk as a single single quote, and the third
single quote restarts the original quotation ended by the
first single quote -- whew!

So to output "hello 'young' lovers" use the following:

awk '
   ...

   print "hello '\''young'\'' lovers"

   ...
' datafile

Ugly, but works.

Make sure the sequence is inside awk double quotes.
Awk doesn't like unquoted single quotes.

jl
--


 4455 Province Line Road        (609) 252-0159
 Princeton, NJ  08540-4322      (609) 683-7220 (fax)



Mon, 22 Jan 2001 03:00:00 GMT  
 Getting awk (or sed or anything else) to put single quotes into file

Quote:

>I'd like to have awk (or anything else for that matter) process a file
>and output part of a line with single quotes around it.
>For example, the input looks like this:
>TEXT   ABC
>(other data omitted)
>and I'd like the output to look like this:
>TEXT   'ABC'
>(other data omitted)

<snip>

Quote:
>Thanks for any help.
>Regards,
>Kevin Outman

The Octal code for a single quote character is 047,
so use that in your Awk program.

For example:

With the input:

TEXT   ABC

You could write something like:

    printf "%s \047%s\047\n", $1, $2
#              ----  ----             <---- shows the "\047" octal codes

I hope that helps.

Best regards,

Lawson Hanson



Tue, 23 Jan 2001 03:00:00 GMT  
 Getting awk (or sed or anything else) to put single quotes into file
Hi,

Try this cmd:

awk '{print xxx $2 xxx}' xxx="'" InputFile

Fred



Tue, 23 Jan 2001 03:00:00 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Putting Single Quotes in the Output...

2. Putting Single Quotes in the Output...

3. Putting single quotes inside Prolog strings

4. REGULAR EXPRESSIONS; PERL/AWK/SED; SINGLE QUOTES/DOUBLE QUOTES

5. Substituting single quotes from Perl via shell using awk and gsub

6. awk not printing single quotes

7. awk: putting quotes around a field value

8. printing single quote (apostrophe) with awk

9. printing a single quote in awk

10. replace single quote to double quote with an example

11. single-quoted string conversion to triple-quoted string

12. log file processing - awk or sed

 

 
Powered by phpBB® Forum Software