473,394 Members | 2,048 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,394 software developers and data experts.

referencing in cmd string

Hi,

I wanted to grep a file for a pattern. The pattern is a dynamic one
stored in the string "rec". I tried to do it as follows:

cmd = "grep -c rec filelist2 > grepdata"
os.system(cmd)

But I find that the system is not referencing the contents of rec
data. There is no syntax error and also the grep itself does not crib.

Please suggest what is the correct method.

Thanks
Jul 18 '05 #1
3 1996
> I wanted to grep a file for a pattern. The pattern is a dynamic one
stored in the string "rec". I tried to do it as follows:

cmd = "grep -c rec filelist2 > grepdata"
os.system(cmd)


You have to actually insert the data in rec into the command-string - the
shell doesn't know anything about your variable rec

This should work:

cmd = "grep -c %s filelist2 > grepdata" % rec

Beware of spaces in rec - maybe you should surround the rec string in the
command by quotes.

Diez
Jul 18 '05 #2

"Kali K E" <ka********@netscape.net> wrote in message
news:ab*************************@posting.google.co m...
Hi,

I wanted to grep a file for a pattern. The pattern is a dynamic one
stored in the string "rec". I tried to do it as follows:

cmd = "grep -c rec filelist2 > grepdata"
os.system(cmd)


I believe you want the value of the string named 'rec', and not 'rec'
itself, inserted in your command string. If that value does not
itself need to be quoted within the command string, then this should
work.

"grep -c %s filelist2 > grepdata" % rec

If your pattern has chars that make it necessary to be (double?)
quoted, then maybe

'grep -c "%s" filelist2 > grepdata' % rec

will work. (The command quoting rules depend on your OS and shell.)

Terry J. Reedy
is what you
Jul 18 '05 #3
Terry Reedy wrote:
If your pattern has chars that make it necessary to be (double?)
quoted, then maybe

'grep -c "%s" filelist2 > grepdata' % rec

will work. (The command quoting rules depend on your OS and shell.)


On Unix commands.mkarg() will apply the proper quoting rules (and add a
preceeding space which does no harm).

'grep -c %s filelist2 > grepdata' % commands.mkarg(rec)

Peter
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: xcomm | last post by:
Hi All, <?php $vars= array("_SERVER","_SERVER","_SERVER","_SERVER","_SERVER","_SERVER"); foreach($vars as $var) { if(isset($$var))echo("$var: ${$var}<br>\n"); } ?> php.net:
4
by: Bosconian | last post by:
I've been researching this for hours and can't seem to find the right syntax. I need to retrieve a value of an array by referencing the element using a string variable. For example: $data...
13
by: Dan Bass | last post by:
Say I've got an ArrayList, which contains a list of integers. Is it possible to get a reference to one of the integers contained within the collection so that changes to it are reflected in the...
6
by: Mikey_Doc | last post by:
Hi We are running cms 2002, Framework 1.0 with Visual studio 2002. We have just upgraded to Framework 1.1 and visual studio 2003. All of our database connection strings are stored within the...
5
by: Amelyan | last post by:
I am struggling here trying to determine what is a good programming practice as far as referencing your URLs. When you use Response.Redirect, do you use 1) Hard-coded string --...
2
by: HankD | last post by:
Hi, I am having a problem with instantiating two custom objects so they DO NOT point to the same memory location. What is happening is that changes I am making to my object1 are changing object2. I...
1
by: DR | last post by:
when i reference a com object by regsvr32 it then find it in the COM tab, it works ok. but when i reference a com object by referencing the .dll file i get this error in my C# application: ...
6
by: Lars Uffmann | last post by:
Hi everyone, I'm trying to execute the same function call with 1 parameter different in a switch statement of a member function. However, if a certain condition is true (and I can check that...
2
by: Andrus | last post by:
I need compile in-memory assembly which references to other in-memory assembly. Compiling second assembly fails with error Line: 0 - Metadata file 'eed7li9m, Version=0.0.0.0, Culture=neutral,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.