Bashar
Bashar

Reputation: 23

replacing second column with first column using awk with multiple separators

I have options file that has something like this:

<option value=Malaysia>xxxxxxxx</option>
<option value=Malawi>yyyyyyyy</option>
<option value=Malta>zzzzzzzz</option>
<option value=Madagascar>hhhhhhhh</option>

using awk i tried to use:

awk -F ">" '{$2=$1;}1' OFS=\> test.html

but it doesn't replace xxxxxxxx with Malaysia due the > separator is considering the entire part before > is the first variable

how to manipulate multiple separators in this scenario so i can replace $2 which i want it to be the xxxxxxxx,yyyyyyyy,zzzzzzzz,hhhhhhhh with $1 which are country names above

Thanks

Upvotes: 1

Views: 373

Answers (2)

Jotne
Jotne

Reputation: 41456

A gnu awk version using gensub

awk -F"=|>" '{print gensub("(>).*(<)","\\1"$2"\\2","g")}' test.html
<option value=Malaysia>Malaysia</option>
<option value=Malawi>Malawi</option>
<option value=Malta>Malta</option>
<option value=Madagascar>Madagascar</option>

Upvotes: 0

anubhava
anubhava

Reputation: 785156

You can use this awk:

awk -F "[<=>]" '{$4=$3; printf "<%s=%s>%s<%s>\n", $2, $3, $4, $5}' test.html

<option value=Malaysia>Malaysia</option>
<option value=Malawi>Malawi</option>
<option value=Malta>Malta</option>
<option value=Madagascar>Madagascar</option>

Upvotes: 3

Related Questions