0

I have the following bash command:

cat setup.py | grep name=`

This returns the line

name='SOME_PROJECTNAME',

How would I pipe this output from grep to just retrieve SOME_PROJECTNAME?

I have tried

cat setup.py | grep name= | tr -d 'name=','

but this removes characters in SOME_PROJECTNAME.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • And/or https://stackoverflow.com/questions/9553715/how-to-get-value-of-variable-config-in-bash, https://stackoverflow.com/questions/15148796/get-string-after-character, https://stackoverflow.com/questions/10358547/how-to-grep-for-contents-after-pattern – Benjamin W. Sep 13 '18 at 17:38
  • See also https://stackoverflow.com/questions/11710552/useless-use-of-cat – tripleee Sep 13 '18 at 17:57

3 Answers3

2

Use grep lookahead.

$ grep -oP "(?<=name=').*(?=')" setup.py
iamauser
  • 11,119
  • 5
  • 34
  • 52
0
#bad cat setup.py | grep name= | cut -d= -f2-
cat setup.py | grep name= | cut -d' -f2
Jack
  • 5,801
  • 1
  • 15
  • 20
0

Sed may be helpful here.

 sed -ne "s/name='\(.*\)'/\1/p" setup.py

The option -n makes sed not print lines by default. Then we replace the entire property line (name='SOME_PROJECTNAME') with the value only (SOME_PROJECTNAME). The p flag in the s/// command makes sed print the line only if the replacement its executed. So, the only line to be printed are the ones where the replacement was done, with the replaced value.

brandizzi
  • 26,083
  • 8
  • 103
  • 158
Jérôme Pouiller
  • 9,249
  • 5
  • 39
  • 47