theGlitchKing theGlitchKing - 6 months ago 17
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:

#!/bin/sh
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
do
{cd /opt/splunk/etc/deployment-apps
INPUT=`grep -r --include "inputs.conf" "\[" | grep -oP '\[[^\]]+'`
echo -n "$i | $INPUT"}

done
echo "";
exit


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

TA-XA6x-Server/local/inputs.conf:[perfmon://Processor]


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

/opt/splunk/etc/deployment-apps/TA-XA6x-Server/local/inputs.conf


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

[perfmon://TCPv4]


Any help would be appreciated. Thank you guys!

Answer

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

#!/bin/bash

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:

#!/bin/bash

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" "\[")