Ishan Joshi Ishan Joshi - 5 months ago 10
Bash Question

Grep command not giving expected output

I am running the below line in a shell script

echo "$(tr -s '\n' ' ' < ${data[1]} | grep -oP '<af:popup.*?"${data[2]}".*?>')"
echo "(tr -s '\n' ' ' < ${data[1]} | grep -oP '<af:popup.*?"${data[2]}".*?>')"


The command is supposed to translate all
\n
from file
${data[1]}
and inside this file a pattern something like this:

(af:popup.*?logicalCostingRecordExistsPopup.*?)


Issue is the first line is returning null data. Just to validate my script, I echoed the command to check what is getting replaced and run in directly in a shell.Output came as below

tr -s '\n' ' ' < hello.jsff | grep -oP '<af:popup.*? logicalCostingRecordExistsPopup.*?>'


When I run it directly in shell, it gives me expected output.

Don't know why it is not giving output when running inside in shell script

Answer

Instead of using double quotes, use single quotes.

echo "$(tr -s '\n' ' ' < ${data[1]} | grep -oP '<af:popup.*?'${data[2]}.*?>')"
Comments