1

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!

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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).

Comments

0

Try combination of awk and grep. Below solution may help Link 1 . Lin 2

1 Comment

Please avoid link-only answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.