Reverse IP address format with sed - linux

I have a txt file with a list of ip addresses against domain names. eg;
1.1.168.192 example1.example1.net
2.1.168.192 example2.example2.net
3.1.168.192 example3.example3.net
.....
12.1.168.192 example12.example12.net
I can't get my sed command to change the output to;
192.168.1.1 example1.example1.net
192.168.1.2 example2.example2.net
192.168.1.3 example3.example3.net
....
192.168.1.12 example12.example12.net
sed command i'm using is
sed -r 's/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/\4.\3.\2.\1/'
using it as
cat filename | sed -r 's/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/\4.\3.\2.\1/'

The only problem is that you've included an anchor $ in your pattern, which tries to match the end of each line but fails. You just need to remove it:
$ sed -r 's/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/\4.\3.\2.\1/' file
192.168.1.1 example1.example1.net
192.168.1.2 example2.example2.net
192.168.1.3 example3.example3.net
Note that I'm passing the file name as an argument to sed, thereby avoiding a useless use of cat.

awk version
awk '{split(".",i,$1);printf "%d.%d.%d.%d %s\n",i[4],i[3],i[2],i[1],$2}' YourFile

$ sed -r 's/([^.]+)(\.[^.]+)(\.[^.]+)\.([^ ]+)/\4\3\2.\1/' file
192.168.1.1 example1.example1.net
192.168.1.2 example2.example2.net
192.168.1.3 example3.example3.net
192.168.1.12 example12.example12.net

Related

sed replace command working but output is invalid

└─$ sed --version
sed (GNU sed) 4.7
file "hosts":
ip1 == 10.0.0.1
ip2 == 10.0.0.100
command executed:
sudo -i sed -i 's/10.0.0.1/10.0.0.200/g' hosts
Output file :
ip1 == 10.0.0.200
ip2 == 10.0.0.20000
The change in ip2 is not expected and an invalid.
This is just a sample logic need this logic for a Cloud Automation in RPA.
The Inputs of ip are dynamic vars.
You need to escape the . and add conditions to avoid partial match:
$ sed 's/== 10\.0\.0\.1$/== 10.0.0.200/' ip.txt
ip1 == 10.0.0.200
ip2 == 10.0.0.100
$ is end of line anchor, if there can be whitespaces at the end of the line, use \s*$
Use ==\s* instead of == if whitespaces after == can vary
I'm assuming you can have only one match per line based on given sample, so g flag isn't used
If ip1, ip2 etc aren't actually part of input, use this:
$ cat ip.txt
10.0.0.1
10.0.0.100
$ sed 's/^10\.0\.0\.1$/10.0.0.200/' ip.txt
10.0.0.200
10.0.0.100
sudo -i sed -i 's/\b10\.0\.0\.1\b/10.0.0.200/g' hosts
escape the . ( . -> . ) because means every character in regex
\b to limit the search to this exact sting
You have to change your command as follow:
sed -i -e 's:10\.0\.0\.1$:10\.0\.0\.200:g' hosts
This both helped me resolve it.
i.e.
$ sudo -i sed -i 's/10.0.0.1\b/10.0.0.200/g' hosts
or
$ sudo -i sed -i 's/<\10.0.0.1\>/10.0.0.200/g' hosts
More Responses are welcome.

How to use sed command to replace hostname and ip adddress

I am using sed command to replace the IP Address of Host from a file where IP is generating randomly. I am not getting a search pattern for a double quote("). below pattern have to replace:
Host1 = "1.1.1.1" replace with Host2="2.2.2.2" where Host1's IP address is not fixed.
I was trying to replace the below file:
cat a.txt
Host1 ="1.1.1.1"
sed -i -e 's/Host1 =*/Host =2.2.2.2/g' a.txt
cat a.txt
Host =2.2.2.2"1.1.1.1"
You can replace 1.1.1.1 by running this command, using sed too:
sed -i 's|"[^"]*"|"2.2.2.2"|' < a.txt
That way the IP address will change to 2.2.2.2 no matter what IP address it was before, as long as it is stored between double quotes (") in your file.

Get lines from a file with a specific pattern in Bash

I have this file:
this is line 1 192.168.1.1
this is line 2 192.168.1.2
this is line 3 192.168.1.2
this is line 4 192.168.1.1
this is line 5 192.168.1.2
I would like to get, in a bash script, all lines (with tabs) which contains, for example, the pattern "192.168.1.1". By this way, I would get:
this is line 1 192.168.1.1
this is line 4 192.168.1.1
But i don't want this result:
this is line 1 192.168.1.1 this is line 4 192.168.1.1
I tried it with sed without success:
var='192.168.1.1'
body=`sed -n -e '/$var/p' $file`
echo $body
Thanks beforehand!
awk to the rescue!
$ awk -v var='192.168.1.1' '$NF==var' file
this is line 1 192.168.1.1
this is line 4 192.168.1.1
Following sed may help you on same.
var="your_ip"
sed -n "/$var/p" Input_file
Or in awk following will help on same.
var="your_ip"
awk -v var="$var" 'var==$NF' Input_file
A simple grep command could do that:
grep 192.168.1.1 filename
Or a more "complex" grep command could do that and write it to another file:
grep 192.168.1.1 filename > new_filename
Hope this helps!
Assuming the data format is well defined like you said:
"this is line <seq> <ip>"
you could do simply this:
cat file | egrep "192.168.0.1$"

Regex to match IP addresses but ignore localhost

So I have this script that does something with IPs allocated to my OS (GNU/Linux) that I get from running ifconfig. It works fine, however, I was wondering if I could filter out loopback/localhost IP (127.0.0.1) in the same regex expression [I assume every server within my cluster has said IP and I don't need to do anything with it in my script.]
What my script uses is:
ifconfig | awk '/(([0-9]{1,3}\.){3})/ {print}' |sed -e "s/.*addr\://g" -e "s/\s.*//g"
I get results like:
> ifconfig | awk '/(([0-9]{1,3}\.){3})/ {print}' |sed -e "s/.*addr\://g" -e "s/\s.*//g"
172.16.0.1
127.0.0.1
I know it might be a stupid question, but could I filter any IP that starts with 127 in my first regex?
I could try changing awk for grep, somethin like:
> ifconfig |egrep -o "addr\:(([0-9]{1,3}\.){3}[0-9]{1,3})" |sed -e "s/.*addr\://g"
but if I try to negate (?!127) at the beginning, bash will interpret it as !127 which would just throw me something from the history.
I mean, I could just run another grep at the end of the oneliner like grep -v "127.0.0.1", but I just wanted to avoid greping something already greped. Not that anything is wrong with that, just trying to know little more and be more efficient, I guess.
With only one grep without sed or awk:
# ip a|grep -oP "inet \K[0-9.]*(?=.*[^ ][^l][^o]$)"
192.168.1.31
172.16.5.31
You can just add a clause to match the 127.0.0.1 and exclude it by adding the next as below. This way Awk ignores doing any action on the lines containing this pattern.
.. | awk '/127.0.0.1/{next}/(([0-9]{1,3}\.){3})/{print}' | ..

bash + how to capture IP address from line

I have many configuration files ,
the line that start with LINE word have IP address
My target to read the line that start with LINE word from the file and print only the IP address
The problem is that IP address can be in any field in the line so I can’t capture the IP according to field number
example
grep LINE file1.txt
LINE /home/Ariate/run.pl "Voda STS 4 Test - " "102841" && ssh 17.77.170.130 -p 2022
grep LINE file2.txt
LINE /home/Ariate/run.pl 137.77.170.30 "Voda STS 4 Test - " "102841" && ssh ACTIVE
please advice how to capture the IP address from the line ( solution can be also with perl one liner )
expected results
echo $IP_FROM_LINE
17.77.170.130
echo $IP_FROM_LINE
137.77.170.30
perl -MRegexp::Common=net -lne 'print $1 if /^LINE.*\b($RE{net}{IPv4})/'
Using this grep -oE:
grep -oE '\d+\.\d+\.\d+\.\d+' file
17.77.170.130
137.77.170.30
OR else:
grep -oP '\d+\.\d+\.\d+\.\d+' file
The following will get you the desired IP addresses:
grep -oP '^LINE.*\b\K\d+\.\d+\.\d+\.\d+' file
To place the result in a variable as request, you'll need to iterate of the results as follows:
grep -oP '^LINE.*\b\K\d+\.\d+\.\d+\.\d+' file |
while read IP_FROM_LINE ; do
echo $IP_FROM_LINE
done
grep -oE '[0-9]{2,3}(\.[0-9]{2,3}){3}'
matches
17.77.170.130
137.77.170.30
or
grep -oP '\d{2}(\.\d{2}){3}'
if your grep supports -P option.
both of them works with the data you have given.
But if you want really worried of what to be matched, use
grep -Eo '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
which would match excat ip addresses.

Resources