Search code examples
pythonsedsubprocessfabric

Python-Fab not reading value from file


sed command not reading value from file.

with open('file.txt') as f:
 content = f.read()
subprocess.call("sed -i '/name/s/$/%s /' copy_vmlist" % content ,shell=True)

above one is not working.sed command should search for the 'name' in copy_vmlist and append the content(jega) of file.txt to next colmun.

Content of file.txt:
jega

Content of copy_vmlist:
Age
name
degree

Expected output in copy_vmlist:
Age
name jega
degree

Solution

  • The problem is in your sed command arguments. I guess you wanted to replace the "name" string to "jega" in the "copy_vmlist" file. Try it:

    #!/usr/bin/python
    import subprocess
    
    with open('file.txt') as f:
      content = f.read().strip()
      cmd = "sed -i 's/name/%s/' copy_vmlist" % content
      subprocess.call(cmd ,shell=True)
    

    Edit:

    After your edit, you need this:

    cmd = "sed -i 's/name/name %s/' copy_vmlist" % content