So, often I’ll come across something that I need done, but don’t want to waste time manually doing it. This particular instance, I had a list of hostnames which I needed to remove the trailing “.” (dot) from the PTR. Normally, if these hostnames were all simple name.TLD domains this would easily be accomplished with cut:

cut -d. -f1-2

This basically means cut by delimiter (-d), where the delimiter is defined as a period (.) and then print fields 1 and 2. Easy enough.

But, what if you have hostnames and subdomains with varying field lengths, such as: something.domain.com. and something.else.domain.com.

So — here’s two easy ways to get it done.

USING VI:
This one’s for you vi fans (and once again pains me to learn another way that vi is more capable than nano/pico):

:%s/.$//g

Run that in vi, and wha-la, all the trailing dots disappear. You can obviously do this for other characters that are not “dots” by replacing the . in the command above.

But wait, there’s still more ways to skin the cat dot.

USING SED:

sed. x=`echo $i|sed s/.$//`

Either way, this leaves you with an easy way to save yourself boatloads of time (depending on how long the list you’re trying to trim is!)

Hope this helps!

-E

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.