Re: Problem while executing "system " in awk
- From: Bob Harris <nospam.News.Bob@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 08 Aug 2006 00:50:22 GMT
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
.
- References:
- Problem while executing "system " in awk
- From: Amit
- Problem while executing "system " in awk
- Prev by Date: Re: Problem while executing "system " in awk
- Next by Date: Syntax error
- Previous by thread: Re: Problem while executing "system " in awk
- Next by thread: Re: Problem while executing "system " in awk
- Index(es):
Relevant Pages
|
|