Issue
I want to read properties from a build file using grep 3.1
in bash (gitbash, actually). Most properties in the file have single line value, some have no value while others like REFLIBS
contains multiline value.
REFLIBS=module.o testModule1.o libutil.a \
libdebug.a \
libxfor.a
SOURCEFILES=client.c
FLAGS=
HEADERS=linkedlist.h
#OLD_REFLIBS ignore lines with bash-style comments
#HEADERS=utils.h
Problem
My grep command:
grep -i -m 1 -E '^REFLIBS=' test.txt
The command captures only up to the first \
, but my goal is to concatenate all lines into a single line like this:
REFLIBS_CAPTURE=$(grep -i -m 1 -E '^REFLIBS=' test.txt)
echo $REFLIBS_CAPTURE
(desired output)
> module.o testModule1.o libutil.a libdebug.a libxfor.a
Before posting I searched again and found this post suggesting to alter default IFS input delimiter from '\n'
to something else, in my case \
. Looks interesting but not sure if this is the right solution.
https://stackoverflow.com/a/44112055/791406
Solution
You can use sed
to delete each backlash-newline sequence, and then match only a single line with grep:
sed -z 's/\\\n *//g' < test.txt | grep -i -m 1 -E '^REFLIBS='
which outputs
REFLIBS=module.o testModule1.o libutil.a libdebug.a libxfor.a
Note that GNU sed
is required for the -z
option, which treats lines as being NUL-separated instead of newline separated. The means that you effectively read the entire input file into memory as a single "line" for sed
to operate on.
Answered By - Brian61354270 Answer Checked By - Timothy Miller (WPSolving Admin)