Using sed in awk column

Hello,

I need to translate a file with more thousand lines like this

zoneobjectrename "AAA_to_BBB"

in a lines

zoneobjectrename "AAA_to_BBB","AAA_BBB"

I tried to using awk and sed in this manner but it don't work

grep _to_ Documenti/zoneSWITCH2 |awk '{print "zoneobjectrename " $2 "," $2= sed 's/_to_/_/' $2}'

Thanks for the help

Hello Alen912,

Following may help you in same.

 awk -F"\"" '{print $1 OFS $2 OFS}' OFS="\""  Input_file
 

Output will be as follows.

 zoneobjectrename "AAA_to_BBB"
 

Thanks,
R. Singh

Thanks RavinderSingh13,
I need to add another columns with the content of $2 but changing _to_ with _ .
Regards

Please use code tags as required by forum rules!

Try

awk '{X=$2; sub(/_to/,"",X);$2=$2 "," X}1' file
zoneobjectrename "AAA_to_BBB","AAA_BBB"

Or for a man sed (linux) solution:

sed -e 's/\(\("[^_]*\)_to_\([^"]*"\)\)$/\1,\2_\3/' file
zoneobjectrename "AAA_to_BBB","AAA_BBB"

Or a shell solution:

while read key word; do echo $key $word,${word%%_*}_${word##*_}; done < file

or, if you prefer perl:

$ perl -e '{while (<>) {chomp;print;print " ".(join ("_",(split "_to_",(split " ",$_)[1])));print "\n"}}' <input.txt
zoneobjectrename "AAA_to_BBB" "AAA_BBB"
zoneobjectrename "AAA_to_BBB" "AAA_BBB"
zoneobjectrename "AAA_to_BBB" "AAA_BBB"
zoneobjectrename "AAA_to_BBB" "AAA_BBB"
zoneobjectrename "AAA_to_BBB" "AAA_BBB"

for the obvious input file.

$ cat alen192.file 
Do not change this line
Change this line zoneobjectrename "AAA_to_BBB"
Do not change this line
zoneobjectrename "AAA_to_BBB"
Do not change this line nor the next
Zoneobjectrename "AAA_to_BBB"

$ perl -pe 's/zoneobjectrename (.{4})_to_(.{4})/$&,$1_$2/' alen192.file 
Do not change this line
Change this line zoneobjectrename "AAA_to_BBB","AAA_BBB"
Do not change this line
zoneobjectrename "AAA_to_BBB","AAA_BBB"
Do not change this line nor the next
Zoneobjectrename "AAA_to_BBB"

The following uses the & like Aia's perl solution

sed 's/\("[^"]*\)_to_\([^"]*"\)/&,\1_\2/' file

The following does not rebuild the $0

awk '{ X=$2; print (sub("_to_","_",X)) ? ($0 "," X) : $0 }' file