Example:
I have a file name example.txt and inside it this text:
some text here INFO 200 cv 58687 http://saomesitehoere.com live connect ASDFG 61.215.80.6 07:16
some text here INFO 100 fv 582702687 http://saomesitehoere.org live connect 31.15.80.1 07:16:33
some text here INFO 00 ov 587 http://saomesitehoere.uk live connect ASGGGGFG 91.211.80.6 09:16
some text here INFO 800 kcv 277 http://saomesitehoere.za live connect AFG 71.215.81.5 09:14
I want to extract the IP-address from the line which contain the string name "ASDFG", meaning 61.215.80.6
Anyone can help ?
$ grep -oP 'ASDFG \K\S*' < file
61.215.80.6
你可以尝试awk:
awk '/\<ASDFG\>/{print $(NF-1)}' file
您可以使用 :
grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
An IP address is like three times one to three digits followed by a dot, followed by one to three digits. So this will give you the IP address in such a line:
$ grep ASDFG logfile | grep -o '\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\+'
61.215.80.6
To extract all the "valid" IP addresses from the file:
gawk '{match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/,a);split(a[0],b,".")} b[1]<=255&& b[2]<=255 && b[3]<=255 && b[4]<=255 &&length(a[0]){print a[0]}' input_file
This will work in two steps:
Note: needs gawk
to use match()
function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.