Jose Torres Jose Torres - 18 days ago 5
Linux Question

find and replace value in SED linux

i have a file called form.php:

<?php
sleep(10);
?>


The purpose is to execute a shell script like this with sed:

$sh example.sh form.php 50


For obtain:

<?php
sleep(50);
?>


I tried this

$cat example.sh
#!/bin/sh
file=$1
sed -i "s/^\( *sleep\)\([^>]*\)</\1$2</" "$1"


but dont work...

Answer

You can use:

sed "s/sleep([^)]*)/sleep($2)/" "$file"

or this one:

sed "s/\(sleep\)([^)]*)/\1($2)/" "$file"

sed use BRE by default and ( or ) take as literal not as special regex meta character.

Comments