1

I use python 2.7.5. I have got some files in the directory/sub directory. Sample of the file1 is given below

Title file name
    path1 /path/to/file
    options path2=/path/to/file1,/path/to/file2,/path/to/file3,/path/to/file4 some_vale1 some_vale2 some_value3=abcdefg some_value4=/path/to/value some_value5

I would like to insert the text /root/directory in the text file. Final outcome i would like to have is as followes:-

Title file name
    path1 /root/directory/path/tofile
    path2=/root/directory/path/to/file1,/root/directory/path/to/file2,/root/directory/path/to/file3,/root/directory/path/to/file4
    options some_vale1 some_vale2 some_value3=abcdefg some_value4=/path/to/value some_value5 

The names path, options and path2 are same in all files. The files in the directory/subdirectory required to be modified with the same outcome as above. I tried to use the re.sub to find and replace the string. However I never got the output i wanted.

2
  • Could you put the re.sub that you have tried? Commented Sep 29, 2013 at 11:36
  • re.sub(r"([ \t =,])/", replace_text, text) where replace_text=/root/directory and text is the content of the file loaded with .read(). As of now i am just trying to figure out replacing/inserting. Getting path2 line in to next line is a different story. Commented Sep 29, 2013 at 11:43

3 Answers 3

1

This one-liner does the entire transformation:

str = re.sub(r'(options) (\S+)', r'\2\n    \1', str.replace('/path/', '/root/directory/path/')

See a live demo of this code

Sign up to request clarification or add additional context in comments.

Comments

0

You can try this:

result = re.sub(r'([ \t =,])/', replace_text, text, 1)

The last 1 is to indicate the first match only, so that only the first path is substituted.

By the way, I think that you want to conserve the space/tab or comma right? Make replace_text like this:

replace_text = r'\1/root/directory/'

3 Comments

That worked perfectely. However i had to remove ad i needed to replace all matching string. Can you tell me what does "\1" does in the r'\1/root/directory/' Now comes the second part. How do i get the path2 line to the next line?
@sundar_ima Sorry, I didn't get notified of your comment :( The \1 matches the captured group, so that you're keeping the part you captured; otherwise, it'd end up as path1/root/directory/path/tofile. Also, that's all right, I thought that path2 was separate and would require a separate re.sub. Are the parts in the options separated by space? And is there always 4 paths and 5 values?
Thanks for the clarification. Yes, option is always seperated by space and even path2=/path/to/file1 also seperated by space. There are some cases where path1, options and path2 are already in different lines.
0

Ok. Got the answer from Bohemian and Jerry. Got it working with combined code.

str = re.sub(r'(options) (\S+)', r'\2\n    \1', re.sub(r'([ \t =,])/', replace_text, text))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.