I have lines like:
Dog Cat House Mouse
Dog Cat House Mouse
Dog Cat House Mouse
Dog Cat House Mouse
I'd like to replace characters only in $3.
H -> Z
s -> W
e -> x
Resulting in something like (where $1, $2, and $4 are not changed):
Dog Cat ZouWx Mouse
Dog Cat ZouWx Mouse
Dog Cat ZouWx Mouse
Dog Cat ZouWx Mouse
I'd guess SED might be best but I'm really new. Sorry to to include any sample code.
awk '{ gsub("H", "Z", $3);
gsub("s", "W", $3);
gsub("e", "x", $3);
print$0}' filename
shell:
while read line;do
set $line
str=${1}" "${2}
tmp=`echo ${3}|sed 's/H/Z/g;s/s/W/g;s/e/x/g;'`
str=${str}" "${tmp}" "${4}
echo ${str}
done < yourfile
perl:
while(<DATA>){
my @tmp=split;
$tmp[2]=~tr/Hse/ZWx/;
print join " ",@tmp;
print "\n";
}
__DATA__
Dog Cat House Mouse
Dog Cat House Mouse
Dog Cat House Mouse
Dog Cat House Mouse
awk 'BEGIN{ w["H"]="Z"; w["s"]="W"; w["e"]="x"}
{ for(i in w){ gsub( i,w,$3) }} 1' file