'How to grep line which contains two commas
I have a file of IP and every line have two or three comma separated IP like -
ip x 171.48.179.64, 194.88.105.83, 10.121.15.191
ip x 122.176.17.76, 194.88.105.83, 10.121.15.191
ip x 223.179.196.169, 10.121.15.135
ip x 157.41.161.64, 10.121.15.135
ip x 49.14.160.119, 10.121.15.191
ip x 157.41.230.108, 10.121.15.191
ip x 101.208.189.88, 194.88.105.83, 10.121.15.191
ip x 180.215.137.150, 194.88.105.83, 10.121.15.191
ip x 157.41.161.64, 10.121.15.191
ip x 157.41.161.64, 10.121.15.191
So I want to grep the lines which contains two comma (three IPs) like -
ip x 171.48.179.64, 194.88.105.83, 10.121.15.191
ip x 122.176.17.76, 194.88.105.83, 10.121.15.191
ip x 101.208.189.88, 194.88.105.83, 10.121.15.191
ip x 180.215.137.150, 194.88.105.83, 10.121.15.191
I searched a lot but did't get any specific answer, please help me.
Solution 1:[1]
In case you have other lines too which could have anything else from IPs then following may help you which will check number of fields as well as make sure 1st, 2nd and 3rd fields are having IPs nothing else.
awk -F, 'NF==3 && \
$1 ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ && \
$2 ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ && \
$3 ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/' Input_file
Solution 2:[2]
you can also use a loop
while read -r line
do
count=${line//[^,]};
if [ ${#count} == 2 ];
then
echo "${line}";
fi
done < your.file
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RavinderSingh13 |
| Solution 2 | AntSt |
