Shell Scripting , need to search and print a line that contains a specific pattern

Take example of below file.

abc.txt

nas1:/abc/test/test1                /test
nas1:/abc/test/test1/test2       /test/abc
nas1:/abc/test/

Now i have a variable that contains " nas1:/abc/test/test1 " value , so i need to search the above file for this variable and print only this line.

output expected :-
nas1:/abc/test/test1 /test

i tried cat abc.txt | grep -w "nas1:/abc/test/test1 " , its not working for few linux servers , so can i have some commands that can run on all distros.

Hello mohit_vardhani,

You could use simple grep option as follows.

grep "nas1:/abc/test/test1"  Input_file

But having said that, above code will give those lines also which have some more text after this line too like above code's output will be as follows.

nas1:/abc/test/test1                /test
nas1:/abc/test/test1/test2       /test/abc

You could use awk for same too.

awk -vVAL="nas1:/abc/test/test1" '($1==VAL)'  Input_file

Where variable's value is stored in above code as named VAL and it is looking for first field to match it, in case you need to change the string and change the matching field you could change above VAL=new_string_value and in place of $1 you could use $2 second field or $3 3rd field etc.

Thanks,
R. Singh

Thanks , its working when running directly from terminal.

But when i am passing variable its not working.
I am using this under remote server

PR="\$( cat filename | awk -vVAL="$my_var" "($1==VAL)")"

Its giving syntax error , any suggestions.

Giving which syntax error? I'll try and guess.

You shouldn't need to put $( ) in quotes, and shouldn't escape the dollar sign.

Also, that's a useless use of cat. awk ... filename ought to do the same thing without the cat.

Actually i am running this command remotely under the script , so it won;t run without escape the dollar sign

I have to grep this from df command , so i am using like below

PR="\$(df -hTP | awk -vVAL="$my_var" ($1==VAL))"

/bin/bash: command substitution: line 11: syntax error near unexpected token `('  
PR="$(df -hTP | awk -vVAL="$my_var" '$1==VAL')"

What exactly is not working? How do the grep versions differ on the various system? How the input files (<TAB> / space separated?)?
Please note the -w wouldn't succeed, as / is NOT a word constituent character.

I am using the code provided above by vgersh99 , and it worked. Can someone please help me to use this command as case insensitive.

I mean it should ignore case sensitiveness.

PR="$(df -hTP | awk -vVAL="$my_var" '$1==VAL')"
PR="$(df -hTP | awk -vVAL="$my_var" 'tolower($1)==tolower(VAL)')"
1 Like

Its working , Thank you so much :slight_smile:

Hi vgersh99 ,

Just need one more help , actually when i am running the below code on mutiple servers for the NFS filesystems , it gets hung , so is there any way we can add a timeout kind of thing to exit from the server where NFS is having issues and continue with next set of servers.

PR="(df -hTP | awk -vVAL="$my_var" 'tolower($1)==tolower(VAL)')"
Moderator comments were removed during original forum migration.

df can hang in statfs(). If you don't need the statfs result (size, used, free) then you better run the non-blocking

mount -v

and post-process it with awk.

This will work , thank you and sorry for not using tags , now i got it.

One more thing , is there any way to timeout any command which is taking more time or hung in script due to any reason. I am using bash.

Some Linux distros have a timeout command.
For example

timeout 60 df ...

See the man page for options.