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 2 Weeks Ago
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 21 June 2017, 12:57 PM EDT
Posts: 711
Thanks: 187
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 2 Weeks Ago
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 27 June 2017, 4:40 PM EDT
Location: Aachen, Germany
Posts: 10,965
Thanks: 280
Thanked 3,371 Times in 3,104 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 (2 Weeks Ago)
Sponsored Links
    #3  
Old Unix and Linux 2 Weeks Ago
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 21 June 2017, 12:57 PM EDT
Posts: 711
Thanks: 187
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 2 Weeks Ago
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 27 June 2017, 4:40 PM EDT
Location: Aachen, Germany
Posts: 10,965
Thanks: 280
Thanked 3,371 Times in 3,104 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 (6 Days Ago), rbatte1 (2 Weeks Ago)
Sponsored Links
    #5  
Old Unix and Linux 2 Weeks Ago
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 27 June 2017, 4:29 PM EDT
Location: Saskatchewan
Posts: 22,256
Thanks: 1,105
Thanked 4,200 Times in 3,885 Posts

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

The Following User Says Thank You to Corona688 For This Useful Post:
mohtashims (6 Days Ago)
Sponsored Links
    #6  
Old Unix and Linux 1 Week Ago
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 27 June 2017, 3:19 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,449
Thanks: 100
Thanked 1,510 Times in 1,120 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 04:41 PM.