I have a file that has a bunch of hostnames correlated with IPs that looks like this:
x-cluster-front-1 192.168.1.2
x-cluster-front-2 192.158.1.10
y-cluster-back-1 10.1.11.99
y-cluster-back-2 10.1.157.38
int.test.example.com 59.2.86.3
super.awesome.machine 123.234.15.6
I want it to look like this:
x-cluster-front-1 192.168.1.2
x-cluster-front-2 192.158.1.10
y-cluster-back-1 10.1.11.99
y-cluster-back-2 10.1.157.38
int-test-example-com 59.2.86.3
super-awesome-machine 123.234.15.6
How can I replace the . (dots) from the first column with – (hyphen) in order to facilitate a sort by the second column? I was thinking of using sed to replace dots until the first space, or replacing every dot but the last three, but I'm having trouble understanding regex and sed. I can perform simple replaces but this is way over my head!
This is part of a larger script that I have been writing in bash. I am stuck at this part.
Best Answer
You can use AWK
Explanation
awk
splits a line on whitespace by default. Thus, the first column of the line ($1
inawk
-ese) will be the one you want to perform the substitutions on. For this purpose, you can use:to perform the required substitution.
Note that
gsub
is supported only forgawk
andnawk
but on many modern distrosawk
is a softlink togawk
.