Finding the pattern and replacing the pattern inside the file

i have little challenge, help me out.i have a file where i have a value declared and and i have to replace the value when called. for example i have the value for abc and ccc. now i have to substitute the value of
value abc and ccc in the place of them.

Input File:

go to &abc=ddd;
 if file found &ccc=10;
 no the value name is &abc;
  and the age is &ccc;

Output:

go to &abc=ddd;
 if file found &ccc=10;
now the value name is ddd;
  and the age is 10;

So what have you tried so far to overcome this challenge?

Going to work on this part again. .

---------- Post updated 07-24-13 at 03:23 AM ---------- Previous update was 07-23-13 at 08:32 AM ----------

I have used following codes but i didnt get the required result:

sed -i "s|${variable}|${value_map[${variable}]}|g" "${input_file}"

---------- Post updated at 06:11 AM ---------- Previous update was at 03:23 AM ----------

I am getting error(command not found) if i use this code: Help me out in this:

#set -x
grep -i "&*=" sample.txt >sample1.txt
#file="Replace_sample.txt"
while read line
do
  var=`echo $line |cut -f1 -d '='`
  val=`echo $var |cut -f2 -d '='`
sed -i  's|${var}|${val}|g' "sample.txt"
done < "sample1.txt"

Using awk:

awk '
        /&/ && /=/ {
                match ( $0, /&.*=/ )
                var = sprintf ( "%s", substr ( $0, RSTART, RLENGTH - 1 ) )
                match ( $0, /=.*;/ )
                val = sprintf ( "%s", substr ( $0, RSTART + 1, RLENGTH - 2 ) )
                A[var] = val
        }
        /&/ && !/=/ {
                match ( $0, /&.*;/ )
                idx = sprintf ( "%s", substr ( $0, RSTART, RLENGTH - 1 ) )
                for ( i = 1; i <= NF; i++ )
                {
                        if ( $i == substr ( $0, RSTART, RLENGTH ) )
                                $i = A[idx]
                }
        }
        1
' file

Hi Yoda,
It is not working for the complex things like the text below: i have 500 to 1000 lines in the txt file which i have to replace.

I have a lerarne  a &lesson=ddd;
which i have to for &mm=22;
asdfasd &G=3;
sad &aa=sss
sor sdfsdf dsfsdfs &lesson
saasdasdasd
d ther asdfadd &lesson
asda &aa
asdasdasd&AA

try also:

awk '
{if ($0 ~/[&].*[=]/) {
    l=$0; gsub("[^0-9A-Za-z& ]", " & ", l);
    c=split(l,a); for (i=1; i<=c; i++) if (a == "=") r[a[i-1]]=a[i+1];
  } else {
    {for (i in r) { if (sub(i, r)) if ($1 == "no") sub("no", "now", $1) }; s=0}
  }
} 1
' sample.txt

And, last but not least, try as well:

awk     'm=match ($0, /&.*=.*;/)        {split (substr($0,RSTART,RLENGTH-1), T, "=");  VAL[T[1]]=T[2]}
         /&/ && !m                      {for (i=1; i<=NF; i++) if ($i in VAL) $i=VAL[$i]}
         1
        ' file

You are not quite consistent between your specifications: sometimes the "variables" are terminated with a ";" sometimes they are not. Remove the -1 from RLENGTH if you want it included.

Assuming that you are using the variable after assigning it on the file...(else it will print a blank in place of &xxx string)

$ perl -pe '$hash{$1}=$2 if /\&(\w+)\=(\w+)/;s/\&(\w++)(?!\=)/$hash{$1}/g;' file
go to &abc=ddd;
 if file found &ccc=10;
 no the value name is ddd;
  and the age is 10;

Hi rdrtx1,

It not working for the generic thing. i dont have same line there will be different lines but with the same format. if you have any idea share it.

---------- Post updated at 03:32 AM ---------- Previous update was at 03:31 AM ----------

Guys if you have idea help me out in this.

Have you tried the perl solution above..?

Hi Rajamadhavan,

Nested quantifiers in regex; marked by <-- HERE in m/&(\w++ <-- HERE )(?!=)/ at -e line 1.

i am getting following error when i run your code

Whats your perl version ? Possessive quantifiers (\w++) works on versions > v5.10. If you can upgrade to perl 5.12 or latest version, it will work...

---------- Post updated at 04:23 AM ---------- Previous update was at 04:14 AM ----------

Try this one..should work in older version (>= 5.8)

perl -pe '$hash{$1}=$2 if /\&(\w+)\=(\w+)/;s/\&((?>\w+))(?!\=)/$hash{$1}/g;' file

Hi Rajamadhavan,

Ya now it works but the it should not replce the undeclared variable tp space. as i need those values.

will get slightly complicated.

perl -pe '$hash{$1}=$2 if /\&(\w+)\=(\w+)/; m/\&((?>\w+))(?!\=)/g;s/\&((?>\w+))(?!\=)/$hash{$1}/g if(defined $hash{$1});' file

Please note the above solutions is under the assumption that you have one assignment and/or one substitution per line.

if you have something like below, we need a bigger script.

&abc value is &bcd

hi raja,

but it is not working in this case.

&mns=cdc
&bbb=34
 
&sss = &mns + &bbb

---------- Post updated at 05:16 AM ---------- Previous update was at 05:10 AM ----------

Hi Raja,

I also have some space on both side of equal ( = ) sign in some case of declaration.

 
&mns = cdc
&bbb = 34
 
&sss = &mns + &bbb

well, i think you need an elaborate script to satisfy all your needs.

Space can be overcome easily, but the assignment statement itself seems to have substitutions of variables. It is very much possible to do this, but not something I can attempt in the middle of my work here.

You can take the lead from all the suggestions posted and try yourself.

Hi Raja,

Thanks for the help man. Since perl is new to me i have asked some things.