Re: Problem while executing "system " in awk



In article
<1154940136.258114.295660@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"Amit" <amitkshukla.r@xxxxxxxxx> wrote:

Hi!
There is a file( named myno) containg phone numbers. Few of them start
with "9891" and few with "9811". I want to create new files names
"9891" and "9811". These files will consist of Numbers mentioned inside
"myno" starting from 9891 or 9891.
I've written following awk prog


{
pno=$1
mystr=substr(pno,1,4)
if ( mystr == 9891 )
{
myfile = 9891
}
if ( mystr == 9811 )
{
myfile = 9811
}
mycmd= "echo " pno " >> " myfile
printf "\n %s",mycmd
system(mycmd)
if ( system(mycmd) != 0 )
printf "rcommand not successful \n"
else
printf " command is successful \n"
}


When I'm executing

awk -f myprog myno

I'm always getting error as

echo 9891199634 >> 9891rcommand not successful

echo 9891417893 >> 9891rcommand not successful

echo 9891194609 >> 9891rcommand not successful


Please suggest why my system() command is not executing


Regards
Amit

Do you have privilege to create a file in the current working
directory? Most likely you do, but the question needed to be
asked.

Wild theory.

The syntax for the >> operator is

>>file
-OR-
number>>file

where number can be another file descriptor you are redirecting
into the file. This would commonly be something like

2>>file

to redirect stderr into the file.

However, depending on your system and the default shell's
behavior, it is possible that the shell is seeing your command as

echo 9891199634>>9891

Yes, I know you put spaces between the >> and the numbers, but if
the shell is ignoring them, then this could be what it looks like
inside the shell.

You might try your commands outside of awk, using /bin/sh as the
shell (/bin/sh is the default shell that the system() command
uses).

You might modify your awk script to enclose the first number in
quotes as an experiment:

mycmd= "echo \"" pno "\" >> " myfile

Ugly, but it should make the command look something like

echo "9891199634" >> 9891

or just use Chris Johnson's far more efficient solution that
doesn't require the system() function at all. And should run much
faster if there is any volume of to myno.

Again, this was all just a wild theory.

Good luck.

Bob Harris
.



Relevant Pages

  • Re: cut command
    ... command is printing them in default manner. ... awk or the cut solution may or may not be what you need. ... If, OTOH, the task is formulated to just use shell features ...
    (comp.unix.shell)
  • Re: Script to extract portions of text from a text file
    ... > But your example is not comparing the same command. ... it wouldn't be sent to the shell until you press enter). ... >> behaves differently in a script and at the prompt ... While awk does: ...
    (comp.unix.shell)
  • Re: best way to evaluate awk result as a shell variable?
    ... That is not a valid shell command. ... Do I need an AWK script or is this something that ... shell before awk has started up. ...
    (comp.lang.awk)
  • comp.unix.shell FAQ - Answers to Frequently Asked Questions Part 2.
    ... character except '\0' is allowed in a file path on Unix. ... the default shell" isn't a good reason. ... command line buffer), and will fail if any file names contain a ... How do I use shell variables in awk scripts ...
    (comp.unix.shell)
  • Re: Removing lines from a plain text file
    ... Apparently there is no simple command. ... > and put prune in a cron job. ... > size by removing the excess from the beginning of the file. ... date from the past and use awk to search through the file for it and discard ...
    (comp.unix.sco.misc)