reading a line and executing a command on that line 
Author Message
 reading a line and executing a command on that line

Dear group

I'm looking for the syntax in awk to execute a command such as a copy on
each line awk reads.

If I pipe the output to another command, that command gets the entire file.
I need each individual line.

e.g awk ' { print $0 } execute a command on $0 ' filename

thanks in advance.

Dave.



Tue, 09 Dec 2003 21:44:05 GMT  
 reading a line and executing a command on that line
all that needs to be done is for the file to be read line by line and each
line to be copied to a different file for example.


Quote:
> Dear group

> I'm looking for the syntax in awk to execute a command such as a copy on
> each line awk reads.

> If I pipe the output to another command, that command gets the entire
file.
> I need each individual line.

> e.g awk ' { print $0 } execute a command on $0 ' filename

> thanks in advance.

> Dave.



Tue, 09 Dec 2003 22:50:20 GMT  
 reading a line and executing a command on that line
Use system():
    system("cp " $0 " outputfile")
or whatever.
Quote:

> Dear group

> I'm looking for the syntax in awk to execute a command such as a copy on
> each line awk reads.

> If I pipe the output to another command, that command gets the entire file.
> I need each individual line.

> e.g awk ' { print $0 } execute a command on $0 ' filename

> thanks in advance.

> Dave.



Tue, 09 Dec 2003 23:37:56 GMT  
 reading a line and executing a command on that line
Thanks for the message

have tried this before but system will not know what is in $0, if I try
system ("echo $0") it will come back with nothing.

I don't just want to do a copy but a series of commands on the lines.

So more like:

awk ' { print $0 }

    execute a command on $0
    commands
    more commands
    etc

Quote:
}' filename

thanks
Dave.


Quote:
> Use system():
>     system("cp " $0 " outputfile")
> or whatever.


> > Dear group

> > I'm looking for the syntax in awk to execute a command such as a copy on
> > each line awk reads.

> > If I pipe the output to another command, that command gets the entire
file.
> > I need each individual line.

> > e.g awk ' { print $0 } execute a command on $0 ' filename

> > thanks in advance.

> > Dave.



Wed, 10 Dec 2003 00:04:55 GMT  
 reading a line and executing a command on that line

Quote:

> Use system():
>     system("cp " $0 " outputfile")
> or whatever.

Clean but not too efective... My mileage is: it's better to create one
"script file" using awk (it's quite fast in this context), and then
execute this script..

--or--

sed -e 's/^/cp /;s/$/ dest/' foo | sh

You can of course make this with (obligatory ;) awk...

awk '{printf("cp %s %s.bak\n",$1,$1)}' foo | sh

BTW: you're on unix, right?

--

Written at:
 # . . .
 . . . #
 . # . .
 . # . .



Wed, 10 Dec 2003 00:22:43 GMT  
 reading a line and executing a command on that line

Quote:

> have tried this before but system will not know what is in $0, if I try
> system ("echo $0") it will come back with nothing.

Then try

    system("echo " $0)

instead. Awk does not interpolate variables inside of string literals.

Quote:
> I don't just want to do a copy but a series of commands on the lines.

Ah, so you're using awk to do the job of a shell. Why not just do this?

    while read line
    do
        ...
    done <infile

If you're running, say, Microsoft Windows and don't have a UNIXish
shell, then consider using an awk script to create batch files
instead of executing commands from within the awk script.

--
Jim Monty

Tempe, Arizona USA



Wed, 10 Dec 2003 00:48:39 GMT  
 reading a line and executing a command on that line
Depending on what you want done, you could do this:
    system("echo " $0)
Though that could cause problems depending on the contents of $0.  Better would
be:
    system("echo '" $0 "'")

If your awk script is only doing the system call to each line, you might
consider using xargs instead.
Ben

Quote:

> Thanks for the message

> have tried this before but system will not know what is in $0, if I try
> system ("echo $0") it will come back with nothing.

> I don't just want to do a copy but a series of commands on the lines.

> So more like:

> awk ' { print $0 }

>     execute a command on $0
>     commands
>     more commands
>     etc
> }' filename

> thanks
> Dave.



> > Use system():
> >     system("cp " $0 " outputfile")
> > or whatever.


> > > Dear group

> > > I'm looking for the syntax in awk to execute a command such as a copy on
> > > each line awk reads.

> > > If I pipe the output to another command, that command gets the entire
> file.
> > > I need each individual line.

> > > e.g awk ' { print $0 } execute a command on $0 ' filename

> > > thanks in advance.

> > > Dave.



Wed, 10 Dec 2003 02:35:14 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Fortran command to read command line argument?

2. Execute python script via C API line-by-line

3. Reading Arguments from Command Line and reading binary data

4. How to execute a command line program in VW

5. Executing Command Line Strings From Ada95/GNAT

6. Executing on the Command Line:

7. Executing from command line

8. Command line execute

9. executing command line programs using tcl in win98

10. Please repost: How to execute MS-DOS command lines from VW

11. how to read text files line by line?

12. reading file line by line

 

 
Powered by phpBB® Forum Software