user3447653 user3447653 - 3 months ago 8
Bash Question

Regular expression matching using shell script

I have created a shell script below that checks whether the file out.txt contains any line with the text "Billing_20160210". I am getting the error in the regular expression part which states that "[[: not found". Not sure whether i am missing something. Any help would be appreciated.

#!/bin/bash
bq ls test:Dataset_test > out.txt
cat out.txt | while read LINE
do
if [ LINE = *"Billing_20160210"* ];
then
echo "Processing $LINE"
fi
done


Edit not working:

#!/bin/bash
bq ls geotab-bigdata-test:JS_test > out.txt
cat out.txt | while read LINE
do
if [[ $LINE == *"DailyBillingTest_20160210"* ]];
then
echo "Processing $LINE"
fi
done

Answer

Don't use a shell loop to filter a file line by line. Use grep as a filter here:

grep 'Billing_20160210' out.txt | while read -r line ; do
    echo "processing ${line}"
done

Or even without a temporary file:

bq ls test:Dataset_test | grep 'Billing_20160210' | while read -r line ; do
    echo "processing ${line}"
done
Comments