Ishan Joshi Ishan Joshi -5 years ago 109
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
from file
and inside this file a pattern something like this:


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 Source

Instead of using double quotes, use single quotes.

echo "$(tr -s '\n' ' ' < ${data[1]} | grep -oP '<af:popup.*?'${data[2]}.*?>')"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download