I have a file with many lines, one is:
COMPOSER_HOME=/home/glen/.composer
I want to extract the string /home/glen/.composer
from this file in my shell script. How can I?
I can get the whole line with grep
but not sure how to remove the first part.
Here:
grep 'COMPOSER_HOME=' file| cut -d= -f2
cut
cut's by delimiter =
and the 2nd portion would be whatever is After the =
eg: /home/glen/.composer
, with -f1
you would get COMPOSER_HOME
Since you tagged linux , you have GNU grep which includes PCRE
grep -oP 'COMPOSER_HOME=\K.+' file
The \\K
means match what comes before, then throw it out and operate on the rest of the line.
You can also use awk
awk -F "=" '$1 == "COMPOSER_HOME" {print $2}' file
Maybe this is enough
sed -nE 's/COMPOSER_HOME=(.*)/\1/p' your_file
It does not print any line unless you explicitly request it ( -n
), it matches the line starting with COMPOSER_HOME=
and captures what follows (.*)
(using ()
instead of \\(\\)
, thanks to -E
), and puts in the replacement only what is captured. Then requests the printing of the line with the p
flag of the s
ubstitution command.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.