Cat/grep just the lines with single ip
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a file something like this
Script http://127.0.0.1/ blabla
127.0.0.1
Script 127.0.0.2/index.html bla bla
127.0.0.2
Script 127.0.0.3/contact bla bla
Script 127.0.0.4/settings bla bla
127.0.0.4
I want to get only the lines that contain just a single ip not all the ips from all the lines.
How can i do this?
the results should be
127.0.0.1
127.0.0.2
127.0.0.4
grep cat
add a comment |Â
up vote
0
down vote
favorite
I have a file something like this
Script http://127.0.0.1/ blabla
127.0.0.1
Script 127.0.0.2/index.html bla bla
127.0.0.2
Script 127.0.0.3/contact bla bla
Script 127.0.0.4/settings bla bla
127.0.0.4
I want to get only the lines that contain just a single ip not all the ips from all the lines.
How can i do this?
the results should be
127.0.0.1
127.0.0.2
127.0.0.4
grep cat
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a file something like this
Script http://127.0.0.1/ blabla
127.0.0.1
Script 127.0.0.2/index.html bla bla
127.0.0.2
Script 127.0.0.3/contact bla bla
Script 127.0.0.4/settings bla bla
127.0.0.4
I want to get only the lines that contain just a single ip not all the ips from all the lines.
How can i do this?
the results should be
127.0.0.1
127.0.0.2
127.0.0.4
grep cat
I have a file something like this
Script http://127.0.0.1/ blabla
127.0.0.1
Script 127.0.0.2/index.html bla bla
127.0.0.2
Script 127.0.0.3/contact bla bla
Script 127.0.0.4/settings bla bla
127.0.0.4
I want to get only the lines that contain just a single ip not all the ips from all the lines.
How can i do this?
the results should be
127.0.0.1
127.0.0.2
127.0.0.4
grep cat
edited Jun 13 at 8:41
asked Jun 13 at 8:37
Killroy2018
346
346
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
The cat
utility just concatenates the data given to it, so it would not be of much use here.
Assuming it's ok to be somewhat sloppy with what constitutes a valid IP address:
grep -Ex '([0-9]1,3.)3[0-9]1,3' file
This would match and print any lines matching the given regular expression (and discard the others).
An even sloppier solution would be to discard lines that contains anything but dots and digits:
grep -v '[^0-9.]' file
Depending on your requirements, one of these would be enough.
For a completely correct regular expression (does not match invalid IP addresses), you could use
grep -Ex '((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]).)3(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])' file
none of those work...
â Killroy2018
Jun 13 at 8:58
cat putty.txt | grep -v '[^0-9.]' Binary file (standard input) matches
â Killroy2018
Jun 13 at 8:59
root@localhost:~# grep -Ex '([0-9]1,3.)3[0-9]1,3' putty.txt root@localhost:~#
â Killroy2018
Jun 13 at 9:03
@Killroy2018 I've tested all of these on the data you provided, and they all work. Ifgrep
is saying that the file is "binary", then you are not working with a text file, and certainly not the file that you posted.
â Kusalananda
Jun 13 at 9:13
sorry mate, you are right, if i test it with my example or similar it works fine. I have to see the particularities of my specific file as i cand give a real sample. It is the saved output from putty, it is text
â Killroy2018
Jun 13 at 9:15
 |Â
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The cat
utility just concatenates the data given to it, so it would not be of much use here.
Assuming it's ok to be somewhat sloppy with what constitutes a valid IP address:
grep -Ex '([0-9]1,3.)3[0-9]1,3' file
This would match and print any lines matching the given regular expression (and discard the others).
An even sloppier solution would be to discard lines that contains anything but dots and digits:
grep -v '[^0-9.]' file
Depending on your requirements, one of these would be enough.
For a completely correct regular expression (does not match invalid IP addresses), you could use
grep -Ex '((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]).)3(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])' file
none of those work...
â Killroy2018
Jun 13 at 8:58
cat putty.txt | grep -v '[^0-9.]' Binary file (standard input) matches
â Killroy2018
Jun 13 at 8:59
root@localhost:~# grep -Ex '([0-9]1,3.)3[0-9]1,3' putty.txt root@localhost:~#
â Killroy2018
Jun 13 at 9:03
@Killroy2018 I've tested all of these on the data you provided, and they all work. Ifgrep
is saying that the file is "binary", then you are not working with a text file, and certainly not the file that you posted.
â Kusalananda
Jun 13 at 9:13
sorry mate, you are right, if i test it with my example or similar it works fine. I have to see the particularities of my specific file as i cand give a real sample. It is the saved output from putty, it is text
â Killroy2018
Jun 13 at 9:15
 |Â
show 1 more comment
up vote
1
down vote
accepted
The cat
utility just concatenates the data given to it, so it would not be of much use here.
Assuming it's ok to be somewhat sloppy with what constitutes a valid IP address:
grep -Ex '([0-9]1,3.)3[0-9]1,3' file
This would match and print any lines matching the given regular expression (and discard the others).
An even sloppier solution would be to discard lines that contains anything but dots and digits:
grep -v '[^0-9.]' file
Depending on your requirements, one of these would be enough.
For a completely correct regular expression (does not match invalid IP addresses), you could use
grep -Ex '((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]).)3(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])' file
none of those work...
â Killroy2018
Jun 13 at 8:58
cat putty.txt | grep -v '[^0-9.]' Binary file (standard input) matches
â Killroy2018
Jun 13 at 8:59
root@localhost:~# grep -Ex '([0-9]1,3.)3[0-9]1,3' putty.txt root@localhost:~#
â Killroy2018
Jun 13 at 9:03
@Killroy2018 I've tested all of these on the data you provided, and they all work. Ifgrep
is saying that the file is "binary", then you are not working with a text file, and certainly not the file that you posted.
â Kusalananda
Jun 13 at 9:13
sorry mate, you are right, if i test it with my example or similar it works fine. I have to see the particularities of my specific file as i cand give a real sample. It is the saved output from putty, it is text
â Killroy2018
Jun 13 at 9:15
 |Â
show 1 more comment
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The cat
utility just concatenates the data given to it, so it would not be of much use here.
Assuming it's ok to be somewhat sloppy with what constitutes a valid IP address:
grep -Ex '([0-9]1,3.)3[0-9]1,3' file
This would match and print any lines matching the given regular expression (and discard the others).
An even sloppier solution would be to discard lines that contains anything but dots and digits:
grep -v '[^0-9.]' file
Depending on your requirements, one of these would be enough.
For a completely correct regular expression (does not match invalid IP addresses), you could use
grep -Ex '((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]).)3(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])' file
The cat
utility just concatenates the data given to it, so it would not be of much use here.
Assuming it's ok to be somewhat sloppy with what constitutes a valid IP address:
grep -Ex '([0-9]1,3.)3[0-9]1,3' file
This would match and print any lines matching the given regular expression (and discard the others).
An even sloppier solution would be to discard lines that contains anything but dots and digits:
grep -v '[^0-9.]' file
Depending on your requirements, one of these would be enough.
For a completely correct regular expression (does not match invalid IP addresses), you could use
grep -Ex '((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]).)3(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])' file
edited Jun 13 at 9:39
answered Jun 13 at 8:44
Kusalananda
101k13199312
101k13199312
none of those work...
â Killroy2018
Jun 13 at 8:58
cat putty.txt | grep -v '[^0-9.]' Binary file (standard input) matches
â Killroy2018
Jun 13 at 8:59
root@localhost:~# grep -Ex '([0-9]1,3.)3[0-9]1,3' putty.txt root@localhost:~#
â Killroy2018
Jun 13 at 9:03
@Killroy2018 I've tested all of these on the data you provided, and they all work. Ifgrep
is saying that the file is "binary", then you are not working with a text file, and certainly not the file that you posted.
â Kusalananda
Jun 13 at 9:13
sorry mate, you are right, if i test it with my example or similar it works fine. I have to see the particularities of my specific file as i cand give a real sample. It is the saved output from putty, it is text
â Killroy2018
Jun 13 at 9:15
 |Â
show 1 more comment
none of those work...
â Killroy2018
Jun 13 at 8:58
cat putty.txt | grep -v '[^0-9.]' Binary file (standard input) matches
â Killroy2018
Jun 13 at 8:59
root@localhost:~# grep -Ex '([0-9]1,3.)3[0-9]1,3' putty.txt root@localhost:~#
â Killroy2018
Jun 13 at 9:03
@Killroy2018 I've tested all of these on the data you provided, and they all work. Ifgrep
is saying that the file is "binary", then you are not working with a text file, and certainly not the file that you posted.
â Kusalananda
Jun 13 at 9:13
sorry mate, you are right, if i test it with my example or similar it works fine. I have to see the particularities of my specific file as i cand give a real sample. It is the saved output from putty, it is text
â Killroy2018
Jun 13 at 9:15
none of those work...
â Killroy2018
Jun 13 at 8:58
none of those work...
â Killroy2018
Jun 13 at 8:58
cat putty.txt | grep -v '[^0-9.]' Binary file (standard input) matches
â Killroy2018
Jun 13 at 8:59
cat putty.txt | grep -v '[^0-9.]' Binary file (standard input) matches
â Killroy2018
Jun 13 at 8:59
root@localhost:~# grep -Ex '([0-9]1,3.)3[0-9]1,3' putty.txt root@localhost:~#
â Killroy2018
Jun 13 at 9:03
root@localhost:~# grep -Ex '([0-9]1,3.)3[0-9]1,3' putty.txt root@localhost:~#
â Killroy2018
Jun 13 at 9:03
@Killroy2018 I've tested all of these on the data you provided, and they all work. If
grep
is saying that the file is "binary", then you are not working with a text file, and certainly not the file that you posted.â Kusalananda
Jun 13 at 9:13
@Killroy2018 I've tested all of these on the data you provided, and they all work. If
grep
is saying that the file is "binary", then you are not working with a text file, and certainly not the file that you posted.â Kusalananda
Jun 13 at 9:13
sorry mate, you are right, if i test it with my example or similar it works fine. I have to see the particularities of my specific file as i cand give a real sample. It is the saved output from putty, it is text
â Killroy2018
Jun 13 at 9:15
sorry mate, you are right, if i test it with my example or similar it works fine. I have to see the particularities of my specific file as i cand give a real sample. It is the saved output from putty, it is text
â Killroy2018
Jun 13 at 9:15
 |Â
show 1 more comment
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449477%2fcat-grep-just-the-lines-with-single-ip%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password