Skip to content
Advertisement

Access the word in the file with grep

I have a conf file and I use grep to access the data in this file but not a very useful method for me.

How can I just get the main word by search-term?

I using:

grep "export:" /etc/VDdatas.conf

Print:

export: HelloWorld

I want: (without “export: “)

HelloWorld 

How can I do that?

Advertisement

Answer

If you’re using GNU grep you can use PCRE and a lookbehind:

grep -P -o '(?<=export:).*' /etc/VDdatas.conf

The -o option means to print only the part of the line that matches the regexp, and using a lookbehind for the export: prefix makes it not part of the match.

You can also use sed or awk

sed 's/export:/s/^export: //' /etc/VDdatas.conf
awk '/export:/ {print $2}' /etc/VDdatas.conf
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement