How do I grep line which contains two commas

Multi tool use
Multi tool use


How do I 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.





how about line number 7 and 8th they also having 2 commas why they are not in output? Please elaborate more.
– RavinderSingh13
Jul 3 at 6:22





Yeah, I have shown two lines for example
– ankit.vishen
Jul 3 at 6:28




2 Answers
2



You can use awk:


awk


awk -F, 'NF==3' file



-F, sets the delimiter to ,. NF is a special variable which contains the number of fields. If the condition NF==3 is met awk will print the current line.


-F,


,


NF


NF==3



With (e)grep it would be something like this:


grep -E '^([^,]+,){2}[^,]+$' file



I would personally use awk because it is much more readable.





@RavinderSingh13 :) Thanks! Well, I still need a few points ...
– hek2mgl
Jul 3 at 6:25





hehe :) thank you!!
– hek2mgl
Jul 3 at 6:29





@hek2mgl second one is not working
– ankit.vishen
Jul 3 at 6:32





Maybe I've overlooked something? I tried it with your test data and that looked good. What exactly is not working?
– hek2mgl
Jul 3 at 6:33





I'm getting the lines which have two IPs
– ankit.vishen
Jul 3 at 6:46



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





@ankit.vishen, please check this solution once too and let me know then.
– RavinderSingh13
Jul 3 at 6:48






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

ydk0I1vXgTpX,31Mh,85 D oGMGV6zHZs
HwWoeu3TsFbwdfu,H,3VQN k2GcreFhWig8kThRj,Y,p43PrQE4FB1,ghE52,CLc4QR79t3EIq8AAd1sAxgpNb

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications