Gerald
Gerald

Reputation: 83

Sorting multiple different string in a file to a outputfile

I am trying to sort all IPs with the same port into one output file. The issue I am having is that with the syntax I use, a lot of wrong IPs will get in the output file:

cat input.txt | grep ":80" > output.port80.txt

Content of the input.txt:

192.168.1.1:8080
192.168.1.2:80
192.168.1.3:18080
192.168.1.4:808
192.168.1.5:80
...

Upvotes: 0

Views: 66

Answers (3)

Tyl
Tyl

Reputation: 5252

If you are using GNU grep, you can use:

grep -P ':80\b' input.txt > output.port80.txt

otherwise, if the file is end in :port, use this:

grep ':80$' input.txt > output.port80.txt

More exactly, if there're white spaces after :port,

grep ':80[[:space:]]*$' input.txt > output.port80.txt

With awk however, you can dealing with situations like 192.168.1.7:80THINGSafter,
and remove the things after the :port:

awk '(p=index($0, ":80")) && (substr($0,p+3,1) !~ /[0-9]/){print substr($0,1,p+2)}' input.txt > output.port80.txt

Upvotes: 1

stack0114106
stack0114106

Reputation: 8711

You can use awk also

$ awk -F: ' /:80$/ { print $0  } ' gerald.log
192.168.1.2:80
192.168.1.5:80

$ awk -F: ' /:80$/ { print $0 > "output." $2 ".log" } ' gerald.log

$ cat  output.80.log
192.168.1.2:80
192.168.1.5:80

$

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133528

Assuming that you need to have only those IPs which are ending with 80 port if this is the case then try following.

grep '.*:80$' Input_file > output_file

Upvotes: 2

Related Questions