0

From below text in file:

current_build: 22
previous_build: 55

I am only trying to grep number value of current_build.

When run following command

grep -o -E '[0-9]+' textfile

The output is both number like below

22    
55

How do i only grep the value 22

4
  • awk '/current_build/{print $2}' file Commented Nov 8, 2018 at 19:44
  • Overlooked your comment. This should be your answer! :) (At least I would mention it in the main answer) Commented Nov 8, 2018 at 20:56
  • Check my answer in "stackoverflow.com/questions/53211019/grep-number-extraction", you just need to replace the words "The total difference is:" by "current_build:". Commented Nov 8, 2018 at 21:02
  • Possible duplicate of Grep number extraction Commented Nov 8, 2018 at 21:03

3 Answers 3

1

With GNU grep:

grep -Po 'current_build: \K.*' file

\K: removes matching part before \K

Output:

22
Sign up to request clarification or add additional context in comments.

Comments

1

I would use awk because it is portable in opposite to grep -o which only works with GNU grep:

awk '/current_build/{print $NF}' file

awk splits the input into fields. The default field separator is a sequence of blank chars. NF is the number of fields, $NF is the last field.

Comments

0

Only print lines matching current_build at the start of the line and show the part after the last space:

sed -n 's/current_build:.* //p' file

Comments

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.