Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Grep for an exact match in a file

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago
Ads89 Ads89 is offline
Registered User
 
Join Date: Jan 2016
Last Activity: 12 July 2017, 5:08 AM EDT
Posts: 26
Thanks: 9
Thanked 0 Times in 0 Posts
Grep for an exact match in a file

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?


Code:
 
 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


Code:
TEST1-AA_test.csv

Any help would be appreciated.

Last edited by Ads89; 1 Week Ago at 07:04 AM..
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 25 July 2017, 1:22 AM EDT
Location: Aachen, Germany
Posts: 11,063
Thanks: 284
Thanked 3,408 Times in 3,138 Posts
Don't use single quotes (which prevent expansion by the shell), but double quotes:

Code:
grep -E "^$variable" file.csv

The Following User Says Thank You to RudiC For This Useful Post:
Ads89 (1 Week Ago)
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago
Ads89 Ads89 is offline
Registered User
 
Join Date: Jan 2016
Last Activity: 12 July 2017, 5:08 AM EDT
Posts: 26
Thanks: 9
Thanked 0 Times in 0 Posts
Still having the same issue even with double quotes - Here is a copy from the log


Code:
+ 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


Code:
 
 grep -E "^${TRIM_FILENAME}" Valid_List.csv

    #4  
Old Unix and Linux 1 Week Ago
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 24 July 2017, 10:22 AM EDT
Location: Lancashire, UK
Posts: 3,168
Thanks: 1,334
Thanked 604 Times in 546 Posts
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?


Kind regards,
Robin
Sponsored Links
    #5  
Old Unix and Linux 1 Week Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 25 July 2017, 1:22 AM EDT
Location: Aachen, Germany
Posts: 11,063
Thanks: 284
Thanked 3,408 Times in 3,138 Posts
WHAT exactly is going wrong? I see the variable is matched and grep returns en exit code of 0 (= success).
Sponsored Links
    #6  
Old Unix and Linux 1 Week Ago
Ads89 Ads89 is offline
Registered User
 
Join Date: Jan 2016
Last Activity: 12 July 2017, 5:08 AM EDT
Posts: 26
Thanks: 9
Thanked 0 Times in 0 Posts
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.


Code:
 
 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

Sponsored Links
    #7  
Old Unix and Linux 1 Week Ago
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 24 July 2017, 8:51 PM EDT
Location: Simplicity
Posts: 3,637
Thanks: 285
Thanked 1,217 Times in 1,100 Posts
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:

Code:
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

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep exact match Cludgie Shell Programming and Scripting 2 05-06-2015 09:44 AM
Searching for exact match using grep ziggy6 Shell Programming and Scripting 2 01-19-2014 02:34 PM
Grep two words with exact match pareshkp Shell Programming and Scripting 1 07-11-2013 10:54 PM
grep exact match rob171171 Solaris 4 03-25-2011 07:59 AM
Exact Match thru grep ????? manas_ranjan UNIX for Advanced & Expert Users 2 08-17-2007 05:57 AM



All times are GMT -4. The time now is 02:32 AM.