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.

How to grep for a string on a FILENAME?

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-13-2017
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 19 August 2017, 7:18 PM EDT
Posts: 741
Thanks: 196
Thanked 7 Times in 7 Posts
Hammer & Screwdriver How to grep for a string on a FILENAME?

I call my bash shell script "test.sh" and pass "admin_usr.txt" as an argument like below.


Code:
./test.sh admin_usr.txt

Inside the "test.sh" i wish to check if the filename passed "admin_usr.txt" i.e "$1" contains the string "admin" or not ... which in this case it does.

Note: I do not wish to search contents of the file but only the filename which is being passed as argument.-.

Can you please help me with the if statement ?
Sponsored Links
    #2  
Old Unix and Linux 06-13-2017
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 20 August 2017, 7:34 AM EDT
Location: Aachen, Germany
Posts: 11,176
Thanks: 287
Thanked 3,444 Times in 3,173 Posts
With more than 700 posts in 6 years, you should at least have a faint idea on how to approach this "problemcito", which you certainly don't hesitate to share so we can discuss...?
The Following User Says Thank You to RudiC For This Useful Post:
mohtashims (06-13-2017)
Sponsored Links
    #3  
Old Unix and Linux 06-13-2017
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 19 August 2017, 7:18 PM EDT
Posts: 741
Thanks: 196
Thanked 7 Times in 7 Posts
Hammer & Screwdriver


Code:
if [ `echo $1 | grep -c "admin" ` -gt 0 ]

But i was looking for something simpler.
    #4  
Old Unix and Linux 06-13-2017
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 20 August 2017, 7:34 AM EDT
Location: Aachen, Germany
Posts: 11,176
Thanks: 287
Thanked 3,444 Times in 3,173 Posts
Still simpler? Not too many options... try

Code:
[ $1 = ${1/admin} ]

The Following 2 Users Say Thank You to RudiC For This Useful Post:
mohtashims (06-21-2017), rbatte1 (06-13-2017)
Sponsored Links
    #5  
Old Unix and Linux 06-13-2017
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 18 August 2017, 6:48 PM EDT
Location: Saskatchewan
Posts: 22,336
Thanks: 1,116
Thanked 4,216 Times in 3,899 Posts

Code:
if echo "$1" | grep -q admin

The Following User Says Thank You to Corona688 For This Useful Post:
mohtashims (06-21-2017)
Sponsored Links
    #6  
Old Unix and Linux 06-14-2017
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 15 August 2017, 4:25 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,526
Thanks: 104
Thanked 1,551 Times in 1,149 Posts
Quote:
Originally Posted by mohtashims View Post
Inside the "test.sh" i wish to check if the filename passed "admin_usr.txt" i.e "$1" contains the string "admin" or not ... which in this case it does.
What i don't understand: if the filename has to contain "admin" and everything else is wrong, why don't you simply add this obligatory string to the parameter passed instead of checking if it is there?

like this:


Code:
filename="admin.${1}"

So if you call the script with


Code:
yourscript.sh foo

The filename would be "admin.foo", etc.

I hope this helps.

bakunin
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 string from files and write to filename when string present in file JC_1 Shell Programming and Scripting 4 08-06-2012 10:52 PM
grep -A 1 "string" filename steve2216 Shell Programming and Scripting 8 09-01-2010 10:18 PM
How to search (grep?) filename for a string and if it contains this then... tuathan Shell Programming and Scripting 4 11-19-2008 10:30 AM
grep string and output filename happyv Shell Programming and Scripting 3 11-19-2007 11:16 PM
Appending to filename a string of text grep finds HLee1981 Shell Programming and Scripting 3 09-06-2005 02:44 PM



All times are GMT -4. The time now is 07:40 AM.