I am currently having some issues while trying to grep for a exact string inside a file. I have tried doing this from command line and things work fine i.e. when no match is found, return code=1 but when its done as part of my script it returns 0 for the same command - I dont know if there is an issue with the variable? Or if its because its part of a loop?
grep -E '^$variable' file.csv
Above is the current code I use but have also tried others but no luck
The variable that I am using is a string that looks something like
Still having the same issue even with double quotes - Here is a copy from the log
+ TRIM_FILENAME=TEST1-AAA_test.csv
+ grep -E '^TEST1-AAA_test.csv' Valid_List.csv
+ '[' 0 = 0 ']'
+ echo -e 'TEST1-AAA_test.csv needs to be sent \n'
TEST1-AAA_test.csv needs to be sent
Perhaps you need to add a trailing $ to your search string. In the way that ^ at the start matches the start of line, a $ at the end matches the end of the line.
Does that help?
Your output is rather confusing in that you don't show us the code. Can we have a look?
Hi Rudi - The problem is that the word I am grepping for, doesnt exist in the file - It is returning 0 when it should actually return 1. From command line it works as expected, but when put into the script it returns with 0.
Below is the code that is being used - Because $TRIM_FILENAME does not exist in $Valid_List, it should not sent the file, but it is.
TRIM_FILENAME=`echo $FILENAME | sed 's,_.._$DATE,,g'`
grep -E "^${TRIM_FILENAME}$" $REFERENCE/$Valid_List
if [ $retcode = 0 ]
then
echo -e "$FILENAME needs to be sent "
hadoop fs -cp $line $OUTPUT_DIR
echo "successfully sent"
else
echo -e "$FILENAME does not need to be sent\n"
exit 8
fi
You certainly want another variable substitution, and the fix is another time double quotes: sed "s,_.._$DATE,,g"
Also it is a good idea to put double quotes around all variables, in order to allow their substitution but not additional expansions. Together with some more suggestions:
TRIM_FILENAME=`echo "$FILENAME" | sed "s,_.._$DATE,,"`
grep -E "^${TRIM_FILENAME}$" "$REFERENCE/$Valid_List"
if [ $? -eq 0 ] # exit status of the previous command
then
printf "%s\n" "$FILENAME needs to be sent"
hadoop fs -cp "$line" "$OUTPUT_DIR"
echo "successfully sent"
else
printf "%s\n\n" "$FILENAME does not need to be sent"
exit 8
fi