Search for string in filename, not file content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for string in filename, not file content
# 1  
Old 04-19-2010
Search for string in filename, not file content

How can I search for a string in a filename? For example, I want to know if a filename (not the file contents) contains the string "test".
# 2  
Old 04-19-2010
Code:
for i in *
do
   echo "$i" | grep -q 'test'  && echo "$i has the word test in it"
done

basically echo the filename thru a pipe to grep.
# 3  
Old 04-19-2010
Quote:
Originally Posted by daflore
How can I search for a string in a filename? For example, I want to know if a filename (not the file contents) contains the string "test".
In what language?

Assuming bourne shell, how about:

Code:
expr index filename len >/dev/null && echo ok

js.

Last edited by zaxxon; 04-19-2010 at 11:11 AM.. Reason: code tags please, ty
# 4  
Old 04-19-2010
Just 1 file name? Is this sufficient?
Code:
$> echo somefilename| awk '/test/ {print "yo"}'
$> echo sometestfilename| awk '/test/ {print "yo"}'
yo

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to prepend filename to its content without a third file?

Hello, Is possibly there a way to prepend the filename to its content without a third file? The reason is to add a header to each file contents to distinguish each other when they are pasted side-by-side. sample.txt: XLOC_001 0 XLOC_002 23 XLOC_003 4 XLOC_012 6output (with the same... (4 Replies)
Discussion started by: yifangt
4 Replies

2. UNIX for Dummies Questions & Answers

Find all files containing string not following symlinks CAT (modified) output content to /filename

This should recursively walk through all dirictories and search for a specified string in all present files, if found output manicured content (eg some regex) with CAT into a specified directory (eg /tmp/) one by one, keeping the original names This is what I have so far, which seems to... (1 Reply)
Discussion started by: lowmaster
1 Replies

3. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 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. Shell Programming and Scripting

How do I search first&second string & copy all content between them to other file?

Hi All, How do I search first string & second string and copy all content between them from one file to another file? Please help me.. Thanks In Advance. Regards, Pankaj (12 Replies)
Discussion started by: pankajp
12 Replies

6. 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

7. Shell Programming and Scripting

shell script to search a string and delete the content

Hi, I've a shell script e.g. #!/bin/bash echo "Enter the next hop id" read nhid echo "enter the IP address" read IP echo "enter the interface name" read name echo "enter the enable/disable state" read state exit 0 now from this script i want to search strings in another (.cam) ... (6 Replies)
Discussion started by: vic_mnnit
6 Replies

8. Shell Programming and Scripting

Adding filename into file content

Dear Experts, Please help to teach me how to add the filename into the file content. Actually the file name are EVENTS-20050912. ***************New output that I want*************** EVENTS-20050912 03:33:37 ALARM: BTSSPAN-277-1 30-18013 EVENTS-20050912 12:10:28 ALARM: BTSSPAN-297-2... (1 Reply)
Discussion started by: missutoomuch
1 Replies

9. UNIX for Dummies Questions & Answers

find filename based on file content

:confused: There is a flat file on my system which contains email addreses of people in my company. This file is utilized when sending notifications for various things. However nobody knows where this file is located or what it is named. The only thing we know is the email address of a user who... (4 Replies)
Discussion started by: kollerj
4 Replies
Login or Register to Ask a Question