0

I have file(file.txt) contains the following

aa=testing
bb=hello
cc=hi

Expected result

the value of aa is testing

How to use grep to find the value of aa?

1
  • grep aa= file.txt, but if you want the output to literally be the value of aa is testing , you can't use grep. Commented Aug 15, 2014 at 7:55

3 Answers 3

1

Use a positive lookbehind in grep:

grep -Po "(?<=aa=).*" file.txt

Output

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

Comments

1
grep -oP 'aa=\K.*' file.txt

Output:

testing

See: http://www.charlestonsw.com/perl-regular-expression-k-trick/

Comments

0
awk -F= '/^aa=/ { print $2 }' file
sed -n '/^aa=/s|^.*=||p' file
sed -n 's|^aa=||p' file

Output:

testing

1 Comment

Some golfing on the awk and you get awk -F= '/^aa/ && $0=$2'

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.