awk split and rename files

I have a file test1.html like below:

<dctm_topnav_en_US>
<html>
.....
</html>
<dctm_topnav_en_CA>
<html>
.....
</html>
<dctm_topnav_en_FR>
<html>
.....
</html>

I need to use awk to split this into three file names like en_US.html ,
en_CA.html, en_FR.html each having content between <html>...</html>

the part I got working is splitting the files using this and getting files
as number

awk '/dctm_topnav/{n++}{ print > n)}' test1.html

But how can I get the file names as the locales and not not a iterative
number?

Try:

awk -F_ '/<dctm/ {f=$(NF-1) FS $NF; sub(">",x,f)}{print > f ".html"}' file
1 Like

Worked like a charm...:b::slight_smile:

Hi Franklin can you please explain what "x" will do here.

x is a not defined empty string, so you could replace x with "".

Regards