How to separate data with varying amounts of underscores?

All, I've searched and haven't found a solution for my particular issue.

I have a file with lines of data that contain varying amounts of underscores imbedded. But all the strings have a final underscore and an interface name:

dmk_hcn_dalian2.XXXX.XXX.XX.COM_Se0/0/0
cn_beijing_terminalserver.XXX.XX.COM_Se0
aurtrpara_1.XXXXX.XXX.XX.COM_Tu108
aurtrpara_2.XXXX.XXX.XX.COM_Tu202
dmk_hcn_dalian1.XXX.XX.COM_Se0/0/0
buranda_2.XXX.XX.COM_Tu215

I need to cut each line at the last underscore, keeping the interface name as one variable, and the Fully Qualified Domain name as another. The problem is that the domain has varying amount of periods as well.
Does anyone have a recommendation to get the output I need with awk/cut/ or sed?

appreciate any suggestions!

Try

sed 's/^\(.*\)_\(.*\)/\1 \2/' file

To put the values in variables try this

sed 's/^\(.*\)_\(.*\)/\1 \2/' file | while read fqdn if
do
echo "fqdn: $fqdn if: $if"
done

Hope this helps.

1 Like

Yes! this works, and I can simply awk the side I want for the variable.
Thanks so much for the quick reply

You're welcome :slight_smile:

Btw, here is another approach, handling it with bash's built-ins only, just in case...

while read line
do
 fqdn=${line%_*}
 if=${line##*_}
# do something with $fqdn and $if here
done <file
1 Like

I verified that the bash built in's work as well. I may use this as its easier to understand how it works. :b: