Reputation: 193
Im begining with bash and I want to find my ip in a .txt file analyzing it. This is an example of part of the file: "Direc. inet:192.****** Difus.:"
The path I think on is searching all the text between "inet:" and " ". My biggest approach until now is getting the entire line with "grep inet:" but I can't figure out how to get just the ip not the entire line with the ip.
Thank you!
Upvotes: 1
Views: 2864
Reputation: 440689
Since you're on Linux, you can take advantage of GNU grep
's -o
and -P
options:
grep -oP '(?<= inet:)[^ ]+' file.txt
Example:
$ grep -oP '(?<= inet:)[^ ]+' <<<'Direc. inet:192.****** Difus.:'
192.******
-o
tells grep
to only output the matching part(s) of each line.
-P
activates support for PCREs (Perl-compatible regular expressions), which support look-behind assertions such as (?<= inet:)
, which allow a sub-expression (inet:
, in this case) to participate in matching, without being captured (returned) as part of the matched string.
[^ ]+
then simply captures everything after inet:
up to the first space char. (character set [^ ]
matches any char. that is not (^
) a space, and +
matches this set 1 or more times).
Upvotes: 0
Reputation: 359
Try combination of awk
and grep
. Below solution may help Link 1 .
Lin 2
Upvotes: 0
Reputation: 242443
Perl to the rescue:
perl -ne 'print $1, "\n" if /inet:([^ ]+)/'
-n
reads the input line by line;[^ ]
matches a character that isn't a space+
means the character must be present one or more times(...)
creates a capture group, the first capture group is referenced as $1
Upvotes: 1