简体   繁体   中英

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

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM