473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

shell command inside perl script

89
I am trying to use a shell grep command inside a perl script using system command as below.

Expand|Select|Wrap|Line Numbers
  1.  system "cat $source_path/s_${i}_${j}_0*testing.txt | grep '     $1'  | head > $path/s_${i}_${j}.txt";
  2.  
I am trying to grep for lines that ends with 1 inside the system command. system command concatenates everything and writing into output file and grep ' $1' does not work. I tried with double quotes too but throwing error. Anything wrong with the syntax?
Nov 30 '10 #1

✓ answered by Oralloy

lilly,

Do yourself a favor and try printing out the command that is being constructed, just to be certain that it is what you want:

Expand|Select|Wrap|Line Numbers
  1. $cmd = "cat $source_path/s_${i}_${j}_0*testing.txt | egrep '1$'  | head > $path/s_${i}_${j}.txt"; 
  2. print $cmd;
I wasn't really not sure why you used $1 as your search pattern. If it's working, then we have to modify your script just a little bit to do the correct insert, otherwise Perl will try to substitute the first pattern match result from the last match (unless that's what you want).

Perhaps try:
Expand|Select|Wrap|Line Numbers
  1. my $pat = chr(9) . '$1';
  2. my $cmd = "cat ${source_path}/s_${i}_${j}_0*testing.txt | grep '     ${pat}'  | head > ${path}/s_${i}_${j}.txt";
  3.  
  4. system $cmd;
Good Luck!!
Oralloy

4 5936
Oralloy
988 Expert 512MB
Lilly,

Please forgive me, as some of my information may be out of date here, but...

First off, the default grep program searches for simple strings - I think you want to use egrep, or one of the grep switches, which enables regular expressions.

Second, your regular expression is probably wrong. You say you want a "1" at the end of a line, which would be the regex '1$'; not the expression ' $1', which probably searches for the six character string consisting of four spaces, a dollar sign, and the numeral one.

Try:
Expand|Select|Wrap|Line Numbers
  1. system "cat $source_path/s_${i}_${j}_0*testing.txt | egrep '1$'  | head > $path/s_${i}_${j}.txt";
as a first cut. If the system complains about lack of egrep, then you'll have to find the correct grep command line switch(es).

Also, please try prototyping your system command from the command prompt - it'll give you a lot better feedback as to why things are (or are not) working. Once your prototype works as desired, then you can hoist it into your Perl code.

Good Luck!
Nov 30 '10 #2
lilly07
89
Hi,
Thx for your response. My data is tab delimited columns. Hence I wanted to grep lines with last column as 1. So I used (control+v then tab key to get
' $1' the last column which ends with 1. I will try with egrep. When I just use a she ll script with above description, it works and hence I constructed as system command inside my perl script. But it doesn't work as my syntax is wrong. Thanks again.
Nov 30 '10 #3
Oralloy
988 Expert 512MB
lilly,

Do yourself a favor and try printing out the command that is being constructed, just to be certain that it is what you want:

Expand|Select|Wrap|Line Numbers
  1. $cmd = "cat $source_path/s_${i}_${j}_0*testing.txt | egrep '1$'  | head > $path/s_${i}_${j}.txt"; 
  2. print $cmd;
I wasn't really not sure why you used $1 as your search pattern. If it's working, then we have to modify your script just a little bit to do the correct insert, otherwise Perl will try to substitute the first pattern match result from the last match (unless that's what you want).

Perhaps try:
Expand|Select|Wrap|Line Numbers
  1. my $pat = chr(9) . '$1';
  2. my $cmd = "cat ${source_path}/s_${i}_${j}_0*testing.txt | grep '     ${pat}'  | head > ${path}/s_${i}_${j}.txt";
  3.  
  4. system $cmd;
Good Luck!!
Oralloy
Nov 30 '10 #4
lilly07
89
Thx Orally for you help. Finally
Expand|Select|Wrap|Line Numbers
  1. egrep '    1\$\'
worked.
Dec 1 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: PzYon | last post by:
Hey 2gether! I'm trying to execute a PERL script on the web server when the user presses a button in an ASP.NET Web Application using Visual Basic. The most obvious solution for me seemed to be...
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
2
by: ¹é¿ø¼® | last post by:
Hello, everybody. I want to call any shell command from php script using shell_exec(). The problem is that the next line of the php script would not be run until a few minutes after running the...
1
by: Nathan | last post by:
This may be a basic task to some of you perl guru's, but I have a problem getting a perl script i've written, when executed by httpd, to send out an e-mail. Basically, i have a few variables...
4
by: nidhikhandelwal | last post by:
I am calling a unix shell script from a perl script by using the following system command: system("./readfile_new.sh"); but it seems the unix script readfile_new.sh is not being called. Can...
9
by: skyy | last post by:
Hi i am trying to use the system() command to run some linux command using perl script... Eg.. Deleting files. system("rm $filename"); However, when my $filename contain " character, the...
0
by: rafiu | last post by:
I have a form that call a perl script and I use shell command to do the calling. The perl script will run some process. I want to be able to disallow user from closing the form when the process is...
9
rajiv07
by: rajiv07 | last post by:
Hi to all, I want execute a perl program inside another perl program with passing some values to called program by calling program. Is any Idea please. Thanks in advance.
4
by: jram01 | last post by:
Hi Folks, I am facing problem to to execute .sql command in sql prompt using perl script. I am able to connect sql promt using " my $odbcinput = "odbcinput.txt"; my...
1
by: Raju Sathliya | last post by:
Hi, I need to run Perforce command from perl script but it gives me error as per below: Can't locate P4.pm in @INC (@INC contains: C:\build\software\Perl\perl5.00402\lib\site ...... my perl...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.