Executing AWK in a perl script using 'system'...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Executing AWK in a perl script using 'system'...
# 1  
Old 01-17-2011
Executing AWK in a perl script using 'system'...

I have a simple perl script that looks similar to this:
Code:
#!/usr/bin/perl/

# Have a lot of PERL code in the front of this script.

#Would now like to execute a system command using AWK

system (qq(cd /location && awk '/full/ {print $1;exit}' /myfile));

The system command in my perl script executes perfectly, where I'm having the issue is the "myfile" has several columns of data, awk will search the file for the word FULL and print the first column of data. Works perfectly at the prompt, but returns entire row when ran in this perl script.

Any ideas why?

Last edited by Scott; 01-17-2011 at 01:58 PM.. Reason: Please use code tags
# 2  
Old 01-17-2011
You can do the entire thing using only perl.. that's the best way ..

use something like this:
Code:
open(DAT,'</location/myfile');

while (<DAT>) {
  chomp;
  if ( /\s*(\w+).*full.*$/ ) {$res=$1;last}
  }
close(DAT);

You will have the result in $res var.

Last edited by Klashxx; 01-17-2011 at 02:07 PM.. Reason: perl code
# 3  
Old 01-17-2011
more info...

Quote:
Originally Posted by Klashxx
You can do the entire thing using only perl.. that's the best way ..
The entire string that I am executing is this:

system (qq(cd /location && awk '/full/ { print $1;exit }' /myfile |xargs tar -cvf /tarball.tgz --bzip2));

Unfortunately, learning perl is not an option at this point in time, simply executing the system command in the perl shell is my only option.

Thanks for great information.
# 4  
Old 01-17-2011
Ok , try this:
Code:
system (qq(cd /location && awk '/full/ { print \$1;exit }' /myfile |xargs tar -cvf /tarball.tgz --bzip2));

# 5  
Old 01-17-2011
Got it!

Here is how I fixed it..

Single q instead of qq...

This calls the entire line of the file into the variable first...
system (qq(cd /location && awk '/full/ {print $1;exit}' /myfile));

This works, it only call the statement as it is written.
system (q(cd /location && awk '/full/ {print $1;exit}' /myfile));

Thanks for the help...
SysAdm2

.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help in executing select query from perl script

Hi, I have a perl snippet that call a select query with a bind variable,when i compile the script I'm unable to get the query output. The same query when i fire in sqlplus fetches few rows. The query takes bit time to fetch results in sqlplus. my $getaccts = $lda->prepare("select distinct ... (1 Reply)
Discussion started by: rkrish
1 Replies

2. Shell Programming and Scripting

executing perl script from another perl script : NOT WORKING

Hi Folks, I have 2 perl scripts and I need to execute 2nd perl script from the 1st perl script in WINDOWS. In the 1st perl script that I had, I am calling the 2nd script main.pl =========== print "This is my main script\n"; `perl C:\\Users\\sripathg\\Desktop\\scripts\\hi.pl`; ... (3 Replies)
Discussion started by: giridhar276
3 Replies

3. Shell Programming and Scripting

Error in executing Perl script

Hello All I am facing an issue The unix script is running fine in unix environment which uses ssh connection but when I try to run the same in informatica environment (same server where I was running the unix script manually successfully) its showing the below error command-line line 0:... (11 Replies)
Discussion started by: Pratik4891
11 Replies

4. Programming

Executing a awk command inside perl script --

Hello experts I want to execute a awk command, which reads from txt files and sums the numbers from the first column for those listed only inside a <init> block -- The awk command is like awk '/<\/?init>/{x = !x}x{a++}x && a > 2{sum+=$1}END{printf"%E" "\n", sum} So, I want to execute... (2 Replies)
Discussion started by: Alkass
2 Replies

5. Shell Programming and Scripting

Executing program with Perl script but needs user input

Hello, I'm running a perl script to execute a program through my Unix command line. The program requires a user input but I want to automatically have perl input the string. Is there a way to do this? Thanks (1 Reply)
Discussion started by: leonard2352
1 Replies

6. Shell Programming and Scripting

Executing script at system start up

Hi I had written a piece of script . Please let me know is it possible to run / execute this script at system startup ?? Thanks in advance . (5 Replies)
Discussion started by: Ravi Pavanv
5 Replies

7. Shell Programming and Scripting

Perl script not executing

Hi All, I have been given a perl script to modify but which is not running completely.And it is not showing any errors also. The script is : #!/usr/local/bin/perl print "Which transaction? "; print "\n"; print "1 - Inquiry"; print "\n"; print "2 - Delete Bank"; print "\n"; print... (8 Replies)
Discussion started by: usha rao
8 Replies

8. Shell Programming and Scripting

Executing a script on a remote system via SSH

Hello all, I have a relatively simple script I wrote to generate a count of errors broken down. What I would like to do is execute this script from another server so that I don't actually have to log in to the server to run the check. The script on what we'll call "Server A" is: ... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

Executing .profile from perl script

Hi, How can i execute .profile from a perl script I need this - i am trying to run perl script from crontab and it looses the environment variables Please provide help Your help is greatly appreciated Thanks (1 Reply)
Discussion started by: prekida
1 Replies

10. Shell Programming and Scripting

Problem executing setuid script in perl-5.8.6

Hi, I have a script (a.pl) that can be run by anyone. The script internally has to read a file and write into few files which are owned by user 'myUser'. Has to read the following file: -rwx------ 1 myuser myuser 4986 Aug 20 18:11 my.file Has to write into following files: ... (0 Replies)
Discussion started by: sarmakdvsr
0 Replies
Login or Register to Ask a Question