[Tutor] implementing sed - termination error

Alan Gauld alan.gauld at yahoo.co.uk
Tue Nov 1 21:19:03 EDT 2016


On 02/11/16 00:18, bruce wrote:

> Trying to do a search/replace over a file, for a given string, and
> replacing the string with a chunk of text that has multiple lines.
> 
> From the cmdline, using sed, no prob. however, implementing sed, runs
> into issues, that result in a "termination error"

I don;t understand what you mean by that last paragraph.
"using sed, no prob" implies you know the command you want
to run because you got it to work on the command line?
If that's correct can you share the exact command you
typed at the command line that worked?

"implementing sed" implies you are trying to write the
sed tool in Python. but your code suggests you are trying
to run sed from within a Python script - very different.

> The error gets thrown, due to the "\" of the newline. 

That sounds very odd. What leads you to that conclusion?
For that matter which \ or newline?
In which string - the search string, the replacement
string or the file content?

> The test file contains 6K lines, but, the process requires doing lots
> of search/replace operations, so I'm interested in testing this method
> to see how "fast" the overall process is.

I'm not sure what you are testing? Is it the sed tool itself?
Or is it the Python script that runs sed? Or something else?

> The following psuedo code is what I've used to test. 

Pseudo code is fine to explain complex algorithms but
in this case the actual code is probably more useful.

> The key point
> being changing the "\n" portion to try to resolved the termination
> error.

Again, I don't really understand what you mean by that.


> import subprocess
> 
> ll_="ffdfdfdfghhhh"
> ll2_="12112121212121212"
> hash="aaaaa"
> 
> data_=ll_+"\n"+ll2_+"\n"+qq22_
> print data_
> 
> cc='sed -i "s/'+hash+'/'+data_+'/g" '+dname
> print cc

I assume dname is your file?
I'd also use string formatting to construct the command,
simply because sed uses regex and a lot of + signs looks
like a regex so it is confusing (to me at least).
But see the comment below about Popen args.

> 
> proc=subprocess.Popen(cc, shell=True,stdout=subprocess.PIPE)
> res=proc.communicate()[0].strip()
> 
> 
> 
> ===================
> error
> sed: -e expression #1, char 38: unterminated `s' command

My first instinct when dealing with subprocess errors is to set
shell=False to ensure the shell isn't messing about with my inputs.
What happens if you set shell false?

I'd also tend to put the sed arguments into a list rather
than pass a single string.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list