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   -   Original Discussion by mohtashims
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 3 November 2017, 12:11 PM EDT
Posts: 779
Thanks: 204
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   -   Original Discussion by mohtashims
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 November 2017, 7:45 AM EST
Location: Aachen, Germany
Posts: 11,659
Thanks: 323
Thanked 3,620 Times in 3,325 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   -   Original Discussion by mohtashims
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 3 November 2017, 12:11 PM EDT
Posts: 779
Thanks: 204
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   -   Original Discussion by mohtashims
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 November 2017, 7:45 AM EST
Location: Aachen, Germany
Posts: 11,659
Thanks: 323
Thanked 3,620 Times in 3,325 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   -   Original Discussion by mohtashims
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 23 November 2017, 4:41 PM EST
Location: Saskatchewan
Posts: 22,523
Thanks: 1,154
Thanked 4,275 Times in 3,948 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   -   Original Discussion by mohtashims
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 24 November 2017, 3:49 AM EST
Location: In the leftmost byte of /dev/kmem
Posts: 5,637
Thanks: 107
Thanked 1,604 Times in 1,181 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 11:52 PM
grep -A 1 "string" filename steve2216 Shell Programming and Scripting 8 09-01-2010 11: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 11:30 AM
grep string and output filename happyv Shell Programming and Scripting 3 11-20-2007 12:16 AM
Appending to filename a string of text grep finds HLee1981 Shell Programming and Scripting 3 09-06-2005 03:44 PM



All times are GMT -4. The time now is 08:46 AM.