0

How to find line startwith and replace in linux.

 file:/usr/test.pl
 startswith:   "abc.xyz" => "0"
 replace:   "abc.xyz" => "1" 

Note before: "abc.xyz" three spaces

I achieved using python script

newlines = []
with open ('/usr/test.pl', 'r') as f:
    for line in f:
        if "\"abc.xyz\" => \"0\"" in line
        newlines.append("   \"abc.xyz\" => \"1\",\n")
    else:
            newlines.append(line)
with open ('/usr/test.pl', 'w') as f:
    for line in newlines:
        f.write(line)

Same how to achieve in bashscript or sed command.

preethy tulpi
  • 415
  • 1
  • 5
  • 15
  • Modifying a script file programmatically is nearly always the wrong approach. You want to refactor your Perl script so it accepts the value for `"abc.xyz"` as a command-line or input parameter, perhaps with `"0"` as the default. – tripleee Feb 04 '18 at 14:39
  • Did you try something like `sed -i 's/^"abc.xyz" => "0"/"abc.xyz" => "1"/' /usr/test.pl` ? Does this works or do you have different requirements ? – Walter A Feb 04 '18 at 14:41

0 Answers0