How to grep for a string on a FILENAME?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep for a string on a FILENAME?
# 1  
Old 06-13-2017
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 ?
# 2  
Old 06-13-2017
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...?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 06-13-2017
Hammer & Screwdriver

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

But i was looking for something simpler.
# 4  
Old 06-13-2017
Still simpler? Not too many options... try
Code:
[ $1 = ${1/admin} ]

These 2 Users Gave Thanks to RudiC For This Post:
# 5  
Old 06-13-2017
Code:
if echo "$1" | grep -q admin

This User Gave Thanks to Corona688 For This Post:
# 6  
Old 06-14-2017
Quote:
Originally Posted by mohtashims
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Usage of grep '^$1' filename

There is a file name list_filenames.dat, this has all the list of all files I need to encrypt, I did not understand what the following syntax is doing: grep -s "^$1" list_filenames.dat, when I manually run this command it just returns all the lines, what is the usage of this ? can someone... (4 Replies)
Discussion started by: okkadu
4 Replies

2. Shell Programming and Scripting

Regex for filename in grep

I want to print the filename keyword="XXTNL_AVSKRIV2ING" ftype="sql' I wan to search the keyword in all the sql files and the output shoul dbe filename:count grep -iwc "$keyword" *.$ftype | grep -v ":0$" But the output does not dispaly the filename which contains space as... (4 Replies)
Discussion started by: millan
4 Replies

3. Shell Programming and Scripting

Diff between grep .* file name and grep '.*' filename

Hi, Can anyone let me know what is difference between grep .* foo.c grep '.*' foo.c I am not able to understand what is exact difference. Thanks in advance (2 Replies)
Discussion started by: SasDutta
2 Replies

4. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

5. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

6. Shell Programming and Scripting

grep -A 1 "string" filename

Dear all, can anyone pls provide equivalent of below code for solaris system.? grep -A 1 "string" filename the above command is working for Linux system. but i need same command for Solaris system thanks (8 Replies)
Discussion started by: steve2216
8 Replies

7. Shell Programming and Scripting

How to search (grep?) filename for a string and if it contains this then...

Hi i want to write a script that will search a filename e.g. test06abc.txt for a string and if it contains this string then set a variable equal to something: something like: var1=0 search <filename> for 06 if it contains 06 then var1=1 else var1=0 end if but in unix script :) (4 Replies)
Discussion started by: tuathan
4 Replies

8. Shell Programming and Scripting

using grep and print filename

Hi, I have a question on bash. Basically I would like to print a file name using bash. I am actually trying to grep a particular character in sequential files. I have alot files such that a.txt, b.txt,c.txt...etc. If I found a certain character, I would print that particular filename. I... (5 Replies)
Discussion started by: ahjiefreak
5 Replies

9. Shell Programming and Scripting

grep string and output filename

Hello, I have over 200 files and some of them have the string like "John price $200". I would like to grep the string. Then output the filename which found the string. I have the following script, but it ONLY output the string echo Please input list file name: read listn for file in `cat... (3 Replies)
Discussion started by: happyv
3 Replies

10. Shell Programming and Scripting

Appending to filename a string of text grep finds

I am wanting to automate a process that includes the step of appending to a filename a string of text that's contained inside the file. I.e. if filename A.fileA contains a string of text that reads 1234 after the phrase ABC, I want the shell script file to rename the file 1234_FileChecked_A.fileA.... (3 Replies)
Discussion started by: HLee1981
3 Replies
Login or Register to Ask a Question