Add shell command to print string 
Author Message
 Add shell command to print string

Summary:

Haven't found clear examples of the following two basic operations
using awk:

1) Add the output of a shell command to a print string ( in this case
   "date"):

FILE contains the lines:
one
two
three

I want to add a fourth line at the begining which contains a text
string plus the current date.
Something like:

Awk 'BEGIN {print "text-string" ADD_DATE_HERE};{print}' FILE

So the output is:

text-string Wed May 12 04:47:25 PDT 1999
one
two
three

2) Modify a line in FILE by adding text string at the begining of that
   line, but only print the modified version:

This command prints the line twice.

awk '/two/{print "something" ,$0};{print}' FILE
one
something two
two
three

I want output like:
one
something two
three



Sun, 28 Oct 2001 03:00:00 GMT  
 Add shell command to print string

Quote:

> Summary:
> Haven't found clear examples of the following two basic operations
> using awk:

have you looked in places like, say, the manual?

Quote:
> 1) Add the output of a shell command to a print string ( in this case
>    "date"):

try using system()

Quote:
> 2) Modify a line in FILE by adding text string at the begining of that
>    line, but only print the modified version:
> This command prints the line twice.
> awk '/two/{print "something" ,$0};{print}' FILE
> one
> something two
> two
> three
> I want output like:
> one
> something two
> three

then restrict the {print} from every line to lines that don't
match /two/

stasinos



Sun, 28 Oct 2001 03:00:00 GMT  
 Add shell command to print string


Quote:

>>Haven't found clear examples of the following two basic operations
>>using awk:

>have you looked in places like, say, the manual?

>>1) Add the output of a shell command to a print string ( in this case
>>    "date"):

>try using system()

system() only adds a shell command's standard output to awk's standard
output. It won't add the shell command's standard output to an awk
variable. The awk statements

cmd = "date"
cmd | getline d
close(cmd)
print "Now it's " d

are an alternative interpretation of what Harry Putnam wants.

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---



Sun, 28 Oct 2001 03:00:00 GMT  
 Add shell command to print string

Quote:
> Awk 'BEGIN {print "text-string" ADD_DATE_HERE};{print}' FILE

> So the output is:

> text-string Wed May 12 04:47:25 PDT 1999
> one
> two
> three

Try this
Awk 'BEGIN {"date" | getline;print "text-string" $0};{print}' FILE

Quote:

> 2) Modify a line in FILE by adding text string at the begining of that
>    line, but only print the modified version:

> This command prints the line twice.

> awk '/two/{print "something" ,$0};{print}' FILE
> one
> something two
> two
> three

try

awk '{if($0 ~ /two/)
       x="something" $0
      else
       x=$0
      print x
     }' FILE

Any more details please email me

Jase



Sun, 28 Oct 2001 03:00:00 GMT  
 Add shell command to print string

Quote:

> system() only adds a shell command's standard output to awk's standard
> output. It won't add the shell command's standard output to an awk
> variable. The awk statements
> cmd = "date"
> cmd | getline d
> close(cmd)
> print "Now it's " d
> are an alternative interpretation of what Harry Putnam wants.

system( "echo \"Now it's $(date)\"" );

works as well, unless you really need to format the date in a way that
`date' won't do for you.

stasinos



Sun, 28 Oct 2001 03:00:00 GMT  
 Add shell command to print string


Quote:

>> system() only adds a shell command's standard output to awk's
>> standard output. It won't add the shell command's standard output
>> to an awk variable. The awk statements

>> cmd = "date"
>> cmd | getline d
>> close(cmd)
>> print "Now it's " d

>> are an alternative interpretation of what Harry Putnam wants.

> system( "echo \"Now it's $(date)\"" );

> works as well, unless you really need to format the date in a way that
> `date' won't do for you.

On my system, system("date \042+Now it's %a %b %e %T %Z %Y\042") gives
the same result with one less process. If you just want to print the
result of a shell command, system() usually suffices _EXCEPT_ if the
shell command's output ends with an unwanted newline. If you want the
shell command's output bracketted on both sides by text with no
intervening newlines, you need to pipe to getline. OK, that's not
technically accurate - you could use system("date | tr -d '\012'") -
but that's straining a bit too hard in order to avoid a pipe to getline.

The pipe example also provides a more general approach that could be
used in more complex situations like

cmd | getline result; print crossreference[result]

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---



Sun, 28 Oct 2001 03:00:00 GMT  
 Add shell command to print string


Quote:
>Summary:

>Haven't found clear examples of the following two basic operations
>using awk:

>1) Add the output of a shell command to a print string ( in this case
>   "date"):

>FILE contains the lines:
>one
>two
>three

>I want to add a fourth line at the begining which contains a text
>string plus the current date.
>Something like:

>Awk 'BEGIN {print "text-string" ADD_DATE_HERE};{print}' FILE

Here's another alternative, pass the date string as a variable:

awk 'NR==1{print "text-string",var}{print}' var="`date`" FILE

there are other methods as others have pointed out.

- Show quoted text -

Quote:
>So the output is:

>text-string Wed May 12 04:47:25 PDT 1999
>one
>two
>three

>2) Modify a line in FILE by adding text string at the begining of that
>   line, but only print the modified version:

>This command prints the line twice.

>awk '/two/{print "something" ,$0};{print}' FILE
>one
>something two
>two
>three

>I want output like:
>one
>something two
>three

If you want to just go to the next input line, use awk's next command,
something like this:

awk '/two/{print "something" ,$0;next};{print}' FILE

of course, you could do the whole thing inside one print statement:

gawk '{print ($0 ~ "two" ? "something " : "") $0}' infile

This uses the ? : operator which is supported in some versions of
awk and gawk.

Note the additional space in "something " and the pasting of
strings within the print command.  You could also have used the
? : inside a printf.

Chuck Demas
Needham, Mass.

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

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



Sun, 28 Oct 2001 03:00:00 GMT  
 Add shell command to print string

Quote:


> > Haven't found clear examples of the following two basic operations
> have you looked in places like, say, the manual?

You may have missed the keyword "clear".  

While reading the manual I quickly found that I am not understanding
very much of it.  Partly because the examples assume the reader has a
working knowledge of basic "awk".  But mostly because I lack even a
basic  background in computer related utilities.

Quote:
> try using system()

The section (in my texinfo "Gawk" manual) dealing with "system()" and
with "strftime" seemed to be explained in some detail but were still
confusing (to me).  Probably quite informative  to someone with a better
education than mine.

I found no examples of a command line usage such as described in my query.
They may be there but I didn't spot them.

I hoped to get some examples here, and with the exception of your
post, I see a number of clear and detailed examples.  

Quote:
> then restrict the {print} from every line to lines that don't
> match /two/

You may not have understood my query on the above.
Since you haven't provided any clear examples.  I'm not sure what you
are suggesting.

Thonk you for the usefull response.



Sun, 28 Oct 2001 03:00:00 GMT  
 Add shell command to print string

Quote:

>Summary:

>Haven't found clear examples of the following two basic operations
>using awk:

>1) Add the output of a shell command to a print string ( in this case
>   "date"):

>FILE contains the lines:
>one
>two
>three

>I want to add a fourth line at the begining which contains a text
>string plus the current date.
>Something like:

>Awk 'BEGIN {print "text-string" ADD_DATE_HERE};{print}' FILE

>So the output is:

>text-string Wed May 12 04:47:25 PDT 1999
>one
>two
>three

Since I see from your other post in the the thread that you're using gawk, then
this will work:

   gawk 'BEGIN{print strftime("text-string %c")}{print}' FILE

No need for a system call at all.  "strftime" called with only one argument
prints that argument using the current time as the reference.  The "%c" in that
argument prints the time in the default system format.  If you look at the man
pages for "date" (or for "strftime" possibly) you'll see that there are many
other choices for telling strftime how to format the date it prints.

Quote:
>2) Modify a line in FILE by adding text string at the begining of that
>   line, but only print the modified version:

>This command prints the line twice.

>awk '/two/{print "something" ,$0};{print}' FILE
>one
>something two
>two
>three

>I want output like:
>one
>something two
>three

Two quick possibilities.
First use a "printf" statment instead of the "print" statement in the first
section, and do not include the newline code ("\n").  Thus:

     gawk '/two/{printf("something ")}{print}' FILE

or explicitly stop yourself from falling through to the catch-all "{print}"
section by including the "next" command in the "/two/" section, like so:

     gawk '/two/{print "something " $0;next}{print}' FILE

Good luck!

Steve Manning
Systems Administrator
Mega Marts Inc.
Milwaukee, WI  USA



Mon, 29 Oct 2001 03:00:00 GMT  
 Add shell command to print string

Quote:

>>The section (in my texinfo "Gawk" manual) dealing with "system()" and
>with "strftime" seemed to be explained in some detail but were still
>confusing (to me).  Probably quite informative  to someone with a better
>education than mine.

Ahhhh.  You're using "texinfo"...  That explains the confusion.

:-)   :-)   :-)   :-)   :-)   :-)   :-)   :-)   :-)   :-)   :-)   :-)

Actually, I have a hard time with texinfo myself.  I don't need to clutter my
already-stretched-to-the-limit memory by remembering another set of interface
commands.  I always install the plain old "man" pages, and use "less" as my
MANPAGER.  Much easier to use, IMHO.

Steve Manning
Systems Administrator
Mega Marts Inc.
Milwaukee, WI  USA



Mon, 29 Oct 2001 03:00:00 GMT  
 Add shell command to print string

Quote:


> >>The section (in my texinfo "Gawk" manual) dealing with "system()" and
> >with "strftime" seemed to be explained in some detail but were still
> >confusing (to me).  Probably quite informative  to someone with a better
> >education than mine.

> Ahhhh.  You're using "texinfo"...  That explains the confusion.

> :-)   :-)   :-)   :-)   :-)   :-)   :-)   :-)   :-)   :-)   :-)   :-)

I acutally read it with Emacs, but still uses most of the commands
you refer to.  But since I use emacs constantly, not a problem.  Also
using emacs to read texinfo documentation means you can bring the full
fire power of emacs to bear.  I usually read the "man" pages in emacs
too.

Try (in emacs) `M-x manual-entry <RET> then enter the man page you
want.  Use all of emacs search techniques, and you can call any
referenced "man" pages by pressing <RET> on the reference.

Quote:
> Actually, I have a hard time with texinfo myself.  I don't need to clutter my
> already-stretched-to-the-limit memory by remembering another set of interface
> commands.  I always install the plain old "man" pages, and use "less" as my
> MANPAGER.  Much easier to use, IMHO.

I didn 't see the examples I wanted there either.  

I think it was more a weak understanding of how awk works than the
medium I'm studying it with.



Mon, 29 Oct 2001 03:00:00 GMT  
 Add shell command to print string

Quote:

> Since I see from your other post in the the thread that you're using
> gawk, then this will work:

>    gawk 'BEGIN{print strftime("text-string %c")}{print}' FILE

Nice... thank you

Quote:

> >2) Modify a line in FILE by adding text string at the begining of that
> >   line, but only print the modified version:
> First use a "printf" statment instead of the "print" statement in the first
> section, and do not include the newline code ("\n").  Thus:

>      gawk '/two/{printf("something ")}{print}' FILE

> or explicitly stop yourself from falling through to the catch-all "{print}"
> section by including the "next" command in the "/two/" section, like so:

>      gawk '/two/{print "something " $0;next}{print}' FILE

Yes, both work well.  With the other examples listed in this thread I
was able to reverse the output by switching places in the command, to
get:

"two something"  instead of:
"something two"  

Your last example can do that too, but not the one with 'printf'    
  print "something " $0 gives  = "something two"
  print $0 " something" gives  = "two something"

{printf("something ")  to
{("something") printf

Gives parse errors.  How can that command be reversed?

**NOTE**

Many thanks to all poster in this thread... These examples make it
much easier to see what is happening.



Mon, 29 Oct 2001 03:00:00 GMT  
 Add shell command to print string


   >Your last example can do that too, but not the one with 'printf'
   >print "something " $0 gives  = "something two"
   >print $0 " something" gives  = "two something"
   >{printf("something ")  to
   >{("something") printf
   >Gives parse errors.  How can that command be reversed?
Same as the print command, you can
printf ($0 "something")  or
printf ("something" $0)

Net-Tamer V 1.08X - Test Drive



Wed, 31 Oct 2001 03:00:00 GMT  
 Add shell command to print string

Quote:

> Same as the print command, you can
> printf ($0 "something")  or
> printf ("something" $0)

You can, but don't. It's bad practice to use printf() and sprintf()
without a constant format string. More precisely, the first argument
of the printf() or sprintf() invocation will always be treated as
a format string, so any characters that are special within a format
string will be treated as such. If any part of the first argument
is a variable, Bad Things can--and eventually always will--happen:

$ echo % | awk '{ printf($0 "something") }'
awk: cmd. line:1: (FILENAME=- FNR=1) fatal: not enough arguments to
satisfy format string
    `%something'
     ^ ran out for this one
$

See? This is better:

    { printf("%ssomething", $0) }

--
Jim Monty

http://www.primenet.com/~monty/
Tempe, Arizona USA



Fri, 02 Nov 2001 03:00:00 GMT  
 Add shell command to print string


Quote:

>> Same as the print command, you can
>> printf ($0 "something")  or
>> printf ("something" $0)

>You can, but don't. It's bad practice to use printf() and sprintf()
>without a constant format string.

True words.  However, I think it was likely that in the original post,
the 'f' was a typo.  Probably the result of having done too much C
programming (Since, note that he says "same as the 'print' command" and
then starts talking about 'printf')

I disagree, BTW, that it is a Bad Thing to use anything other than a
constant string for the format - although it is probably good advice
for newbies.



Fri, 02 Nov 2001 03:00:00 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. getting strings from the shell commands

2. Escaping strings to be used in shell commands?

3. fortran command instead of shell command ?

4. Info commands does not show extension added commands?

5. Adding strings manually to an array of strings

6. Problem - Adding a List Box to a dialog Shell

7. Add more functionality to shell/awk script

8. Adding shell arrays (is it possible?)

9. How do I execute a shell command and capture the standard output

10. command shell

11. Substituting field with the result of a shell command

12. Passing command line variables to AWK in shell script

 

 
Powered by phpBB® Forum Software