theGlitchKing theGlitchKing - 5 months ago 7
Linux Question

Reformatting name / content pairs from grep in a bash script

I'm attempting to create a bash script that will grep a single file for two separate pieces of data, and print them to stdout.

so far this is what I have:

cd /my/filePath/to/directory

APP=`grep -r --include "inputs.conf" "\[" | grep -oP '^[^\/]+'`
INPUT=`grep -r --include "inputs.conf" "\[" | grep -oP '\[[^\]]+'`

for i in $APP
{cd /opt/splunk/etc/deployment-apps
INPUT=`grep -r --include "inputs.conf" "\[" | grep -oP '\[[^\]]+'`
echo -n "$i | $INPUT"}

echo "";

Which gives me an output printing the entire output of the first command (which is about 200 lines) then a | then the other results from the second command. I was thinking I could create an array to do this, however i'm still learning bash.

This is an output example from the command without piping to grep


there are 200+ of these in a single execution, and I was looking to have the format be printed as something like this

app="TA-XA6x-Server/local/inputs.conf:" | input="[perfmon://Processor]"

There are essentially two pieces of information i'm attempting to stitch together:

  1. the file path to the file

  2. the contents of the file itself (the input)

Here is an example of the file path


and this is an example of the inputs.conf file contents:


Any help would be appreciated. Thank you guys!


The easy, mostly-working-ish approach is something like this:


while IFS=: read -r name content; do
  printf 'app="%s" | input="%s"\n' "$name" "$content"
done < <(grep -r --include "inputs.conf" "\[")

If you need to work reliably with all possible filenames and have GNU grep available, consider the following instead:


while IFS= read -r -d '' name && IFS= read -r content; do
  printf 'app="%s" | input="%s"\n' "$name" "$content"
done < <(grep -r --null --include "inputs.conf" "\[")