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 1 Week 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 1 Week Ago
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 June 2017, 9:52 AM EDT
Location: Aachen, Germany
Posts: 10,953
Thanks: 277
Thanked 3,367 Times in 3,100 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 (1 Week Ago)
Sponsored Links
    #3  
Old Unix and Linux 1 Week 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 1 Week Ago
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 June 2017, 9:52 AM EDT
Location: Aachen, Germany
Posts: 10,953
Thanks: 277
Thanked 3,367 Times in 3,100 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 (2 Days Ago), rbatte1 (1 Week Ago)
Sponsored Links
    #5  
Old Unix and Linux 1 Week Ago
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 23 June 2017, 6:44 PM EDT
Location: Saskatchewan
Posts: 22,254
Thanks: 1,105
Thanked 4,197 Times in 3,883 Posts

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

The Following User Says Thank You to Corona688 For This Useful Post:
mohtashims (2 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: 22 June 2017, 5:05 AM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,446
Thanks: 100
Thanked 1,509 Times in 1,119 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 10:00 AM.