bash - grep to display all IPs in a text file (linux)
2014-07
I'm writing a script to look for a ping from a certian foreign IP, but I am having some trouble understanding how to format my "grep" command.. Inside my text file is the output from "netstat anp" command. In windows, I was able to achieve this with the following command:
grep -oP (\d{1,3}\.){1,3}\d{1,3} inc.txt
The output in the text file is formatted as such:
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 1 172.18.24.12:51699 72.21.91.17:https FIN_WAIT1
tcp 0 1 172.18.24.12:55154 71.19.176.223:https FIN_WAIT1
tcp 0 1 172.18.24.12:59926 173.194.46.89:http FIN_WAIT1
(I plan of filtering the local address out). But I can't seem to figure out how I should be formatting this command for my bash script. I don't suppose somebody here can help me out with this? Help is always appreciated.
Thanks in advance.
Just quoting the regex seems to work for me:
grep -oP '(\d{1,3}\.){1,3}\d{1,3}' inc.txt
A successive discovery method that works from a script or from a shell is this:
For example suppose we are looking for 72.21.95.46
grep ' 72\.' netstat.txt will give you all the ip addresses that start with 72.
you can then filter that output with subsequent greps to limit the IPs to a family or unique IP:
from the shell you can use "repeat the previous command with bang bang":
!! | grep 21\.
from a script
grep ' 72\.' | grep 21\.
and so on.
To get it in one shot just say
grep 72.21.95.46 netstat.txt
the dots will expand to any single character
if you are flying space shuttles use:
grep '72\.21\.95\.46' netstat.txt
I've been trying for about the past 30 minutes to get this to work properly. grep
is not exactly the most difficult thing to use, so I'm somewhat baffled as to why this won't work.
The files I'm trying to use grep
on are simple XHTML log files. Their names are in the format [email protected], though I don't think that should matter, and inside is simple XHTML.
I copied one such log file to be testfile
so you can see the output of some commands and why it's baffling to me:
[~/.chatlogs_windows/dec] > whoami
reid
[~/.chatlogs_windows/dec] > type grep
grep is /bin/grep
[~/.chatlogs_windows/dec] > uname -a
Linux reid-pc 2.6.35-22-generic #33-Ubuntu SMP Sun Sep 19 20:32:27 UTC 2010 x86_64 GNU/Linux
[~/.chatlogs_windows/dec] > cat /etc/issue
Linux Mint 10 Julia
[~/.chatlogs_windows/dec] > ls -lh testfile
-rw-r--r-- 1 reid reid 63K 2011-01-10 12:45 testfile
[~/.chatlogs_windows/dec] > tail -3 testfile
</body>
</html>
[~/.chatlogs_windows/dec] > file testfile
testfile: XML document text
[~/.chatlogs_windows/dec] > grep html testfile
[~/.chatlogs_windows/dec] > grep body testfile
[~/.chatlogs_windows/dec] > grep "</html>" testfile
[~/.chatlogs_windows/dec] > grep "</body>" testfile
[~/.chatlogs_windows/dec] > cat testfile | grep html
[~/.chatlogs_windows/dec] > cat testfile | wc -l
231
[~/.chatlogs_windows/dec] > cat testfile | tail -3
</body>
</html>
[~/.chatlogs_windows/dec] > chmod a+rw testfile && ls -lh | grep testfile
-rw-rw-rw- 1 reid reid 63K 2011-01-10 12:45 testfile
[~/.chatlogs_windows/dec] > grep html testfile
That's what I'm attempting to do. I want to just use grep -ri query .
in ~/.chatlogs_windows
, which normally works perfectly for me... but for some reason, it completely fails at going through these files.
If it matters, I copied these files off of my Windows 7 partition. But I chown'd them and gave myself all the appropriate permissions, and other programs (like cat
) seem to read them just fine. I also copied testfile to testfile_unix and converted the line endings and tried that, but it didn't work either.
I'm using zsh, but I tried it on bash and that failed too. Also, grep works normally: I tried it out on my documents folder and it worked flawlessly.
If you need any more information, just let me know. I tried googling around, but I found no reason for grep to simply not work. Thanks in advance.
The grep tool doesn't recognise the UTF-16 file encoding.