Bash script – Get part of a line of text from another file


I’m quite new to bash scripting. I have a script where I want to extract part of the value of a particular line in a separate config file and then use that value as a variable in the script.
For example:

Line 75 in a file named config.cfg


I want just the value at the end of "thecert.cer" to then use in the script. I’ve tried awk and various uses of grep but I can’t quite get just the name of the certificate.

Any help would be appreciated. Thanks

These are some examples of the commands I ran:

awk -F "/" '{print $4}' config.cfg
grep -o *.cer config.cfg

Is this possible to extract the value on that line and then edit the output so it just contains the name of the certificate file?


This is a pure Bash version of the basic functionality of basename:


which removes everything up to and including the final slash. It presupposes that you’ve already read the line.

Or, using sed:

cert=$(sed -n '75s/^.*\///p' filename)


cert=$(sed -n '/^ssl_cert_location=/s/^.*\///p' filename)

This gets the specified line based on the line number or the setting name and replaces everything up to and including the final slash with nothing. It ignores all other lines in the file (unless the setting is repeated in the case of the text match version). The text match version is better because it works no matter what line number the setting is on.

grep uses regular expressions (as does sed). The grep command in your command appears to have a glob expression which won’t work. One way to use grep (GNU grep) is to use the PCRE feature (Perl Compatible Regular Expressions):

cert=$(grep -Po '^ssl_cert_location=.*/\K.*' filename)

This works similarly to the sed command.

I have anchored the regular expressions to the beginning of the line. If there may be leading white spaces (the line may be indented), change the regex so it looks something like this:


which works for both indented and unindented lines.

Answered By – Dennis Williamson

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published