-1

In a Jython script I am trying to replace an entire line of a text file (the 2nd line in this case) with a directory address that contains a variable (VAR).

obsids=str(obsids[i])
print type(obsids)
os.system('sed -i "2 s:.*:/usr/data/$obsids/:" /usr/software/file.txt')

Where obsids was a unicode of a whole number, then a string (usually something like 1342204440).

Please can you show me the error in my script.

Thanks.

  • If you want to use the variable and not the text `$VAR`, then you need to double quote the `sed` command. – fedorqui Oct 07 '13 at 15:31
  • Your Python snippet is completely broken, and the error message is unreadable too. – Erik Kaplun Oct 07 '13 at 15:35
  • @fedorqui thanks, I don't get the error message now and the script runs, but it doesn't execute the sed command as desired – That Wilso Kid Oct 08 '13 at 09:14
  • @ErikAllik what do you mean? It seems that fedorqui and myself can see it :/ – That Wilso Kid Oct 08 '13 at 09:15
  • @ThatWilsoKid Please update your question showing the exact code your are using, as well as the value of `$VAR` (maybe it contains a `/` so it conflicts with the sed delimiter) – fedorqui Oct 08 '13 at 09:15
  • @fedorqui Updated, I thought it could be an issue with the variable being a unicode not a string, but that doesn't seem to alter it not working – That Wilso Kid Oct 08 '13 at 09:35
  • Mmmm first of all, what `sed` replacement you want to do? I guess you want to replace everything up to `:` with `/usr/data/$obsids/:`. Note that you need `"` instead of `'` to have the $obsids variable expanded. – fedorqui Oct 08 '13 at 09:42
  • @fedorqui I would like to replace everything on the 2nd line in file.txt with `/usr/data/$obsids/` – That Wilso Kid Oct 08 '13 at 09:48
  • @ThatWilsoKid so `sed "2 s:.*:/usr/data/$obsids/:" your_file` should make it. – fedorqui Oct 08 '13 at 10:07
  • @fedorqui updated to your sugestion, it now replaces the correct line in the text file, but without the `obsids`, does it matter what type `obsids` is? – That Wilso Kid Oct 08 '13 at 10:14
  • But what do you want? `$obsids` to be this exact value or to use the value of `obsids` variable? – fedorqui Oct 08 '13 at 10:16
  • @fedorqui the value of the `obsids` variable – That Wilso Kid Oct 08 '13 at 10:20
  • @ThatWilsoKid I guess the variable is not set properly. Does it work to you if you do `os.system('obsid="testvalue"; sed "2 s:.*:/usr/data/$obsids/:" /usr/software/file.txt')`? – fedorqui Oct 08 '13 at 10:23
  • @fedorqui you are correct, it returns as /usr/data/testvalue/ so how do I set the variable properly? – That Wilso Kid Oct 08 '13 at 10:34
  • @ThatWilsoKid I think it is a matter of using `os.environ['obsids']=obsids`. I just posted it as an answer to make it more clear. – fedorqui Oct 08 '13 at 10:45

1 Answers1

0

We got it in the comments:

obsids=str(obsids[i])
print type(obsids)
os.environ['obsids']=obsids  # this makes the Python variable behave inside UNIX
os.system('sed -i "2 s:.*:/usr/data/$obsids/:" /usr/software/file.txt')
fedorqui
  • 275,237
  • 103
  • 548
  • 598