lifzgud
November 24, 2010, 8:58am
1
Hi ,
I have a requirement where i have this file
abcd
$$xyz=123
$$zef=789
$$mno=123
$$pqr=456
I need to replace $$pqr from 456 to a new value which will be present in a variable to me.
I am not aware of sed and awk functionality.
Can somebody please show me how to replace this 456 with a new value .
perl -e -pi s/456/'$var'/g infile
If pqr can have any numeric value you can use:
sed 's/\(^$$pqr=\)[0-9][0-9]*$/\1'"$VAR"'/' infile
alternative in sed..
sed '/$$pqr/s/[0-9]\+/'$VAR'/' inputfile > outfile
And another one with awk:
awk -v var=$variable -F= '/\$\$pqr/{$2=var}1' OFS=\= infile
sed '/$$pqr=/'"s/=.*/=$var/" infile
sed "/\$\$pqr=/s/=.*/=$var/" infile
Or using Perl -
$
$ cat f42
abcd
$$xyz=123
$$zef=789
$$mno=123
$$pqr=456
$
$
$ typeset -x VAR="999"
$
$ # Perl one-liner to substitute the value
$ perl -lne 'if (/^(\$\$pqr=).*/){print $1,$ENV{"VAR"}} else {print}' f42
abcd
$$xyz=123
$$zef=789
$$mno=123
$$pqr=999
$
$
tyler_durden