How to chk the file name using wildcards??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to chk the file name using wildcards??
# 1  
Old 05-29-2008
Question How to chk the file name using wildcards??

Hi,

I need to find a file with name xyz.mno.1234235.msg

Here the numbers can be anything and is not fixed. But the first 6 characters are fixed ie, xyz and mno are fixed and wont change. Actually this file comes to this directory every week with only the number part changing.

How is it possible to check whether the file is present and if it is present then how to fetch only the numbers to a variable? Is it possible in shell script.

The script which checks for this file and the above file are in the same directory.


Thanks,
RRVARMA
# 2  
Old 05-29-2008
Question Can you be more specific in what you are trying to accomplish?

looking at how some wildcards could work
Code:
>ls -l xyz.mno.???????.msg
-rw-rw---- 1 user dp 0 May 29 08:17 xyz.mno.1234235.msg

>ls -l xyz.mno.???????.msg | tr -s " " | cut -d" " -f9
xyz.mno.1234235.msg

and then finally
Code:
>ls -l xyz.mno.???????.msg | tr -s " " | cut -d" " -f9 | cut -d"." -f3
1234235

explained:
list files
translate characters, squeeze out extra " " space characters
set delimiter to " " and get the 9th field
set delimiter to "." and take the 3rd field

or, to a variable
Code:
>filenm=$(ls -l xyz.mno.???????.msg | tr -s " " | cut -d" " -f9 | cut -d"." -f3)
>echo $filenm
1234235

# 3  
Old 05-29-2008
Not using the -l option saves you from nearly all of the gyrations having to do with extracting the information that you do want. Just drop the -l and the script becomes significantly simpler.

Actually, you could simply use echo with a wildcard and look at the result. Depending on your shell and its settings, the result when the wildcard doesn't match can be either an empty string, or the wildcard expression unexpanded. If it's anything else, the file is there, and the result is the result of the wildcard expansion.

Code:
file=`echo xyz.mno.*.msg`
case $file in
  '' | 'xyx.mno.*.msg') echo no file found;;
  *) echo file $file found;;
esac


Last edited by era; 05-30-2008 at 04:13 AM.. Reason: Fixed embarrassing typo
# 4  
Old 05-29-2008
Quote:
Originally Posted by RRVARMA
How is it possible to check whether the file is present and if it is present then how to fetch only the numbers to a variable?
Code:
var=`ls xyz.mno.*.msg | awk -F. '{print $3}'`

# 5  
Old 05-30-2008
find . -name "abc.def.[1-9][1-9][1-9].txt"

It will search the file have name like abc.def.123.txt (Inplace of 123 it can be any 3 digit number).
# 6  
Old 05-30-2008
MySQL Thanks very much.. Problem solved..

Hi All,

Thank you very much.. I'd get a solution because of you all..

Special thanks to Joeyg.. because i too was trying the same way but in vain.. after getting your reply i could understand where i went wrong and correct myself.. Thanks very much..

Thanks to Era, Danmero, Siba.S.Nayak for giving me other ideas to implement this.. Thanks a lot.. Smilie

RRVARMA
# 7  
Old 07-10-2008
find . -type f -name "xyz.mno.[0-9]*.msg"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Wildcards in file input to a script?

I have four files: test test2 test3 test4 I have this simple script: #!/bin/bash ls $1 Why does ./the_script.sh test* only list the first file, when a normal ls test* would list all four? What do I need to change in the script to be able to use wildcard? (12 Replies)
Discussion started by: KidCactus
12 Replies

2. Shell Programming and Scripting

SH Script for file name wildcards

Does anyone know how I would go about inserting text at the beginning of a file with the file name containing a daily time stamp? Essentially I need to find the file name using a wild card, and then insert 3 lines of text - one of which is the processing date. Help please!? (1 Reply)
Discussion started by: cookie33
1 Replies

3. UNIX for Advanced & Expert Users

Wildcards

These 2 websites do a GREAT job of explaining different types of wildcards. I learned about the categories of characters which I never knew about at all. GNU/Linux Command-Line Tools Guide - Wildcards GREP (1 Reply)
Discussion started by: cokedude
1 Replies

4. UNIX for Advanced & Expert Users

dpkg wildcards

Are there different rules with wildcards in dpkg? I was looking at this. Getting information about packages % dpkg -l \*apt\* Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Installed/Config-files/Unpacked/Failed-config/Half-installed |/... (1 Reply)
Discussion started by: cokedude
1 Replies

5. Shell Programming and Scripting

Check for file existence using wildcards

I am using the following command to check for files on a Unix (Solaris 9) and on Linux: if (-r *.) then echo " las file found" else echo " no las file found" endif If no las file is present, the "no las file found" message is displayed. If a las file is present, however, I get... (9 Replies)
Discussion started by: phudgens
9 Replies

6. UNIX for Dummies Questions & Answers

File names based off of wildcards

Hello all- First post, so just to forewarn you: I know enough about Perl and the Terminal to get myself into trouble, not quite enough to always get out. I'd like to know if it is possible to, from the command prompt, use a wild-card to declare the names of files for input and then use the... (1 Reply)
Discussion started by: tcquad
1 Replies

7. UNIX for Dummies Questions & Answers

wildcards NOT

Hi All Please excuse another straightforward question. When creating a tar archive from a directory I am attempting to use wildcards to eliminate certain filetypes (otherwise the archive gets too large). So I am looking for something along these lines. tar -cf archive.tar * <minus all *.rst... (5 Replies)
Discussion started by: C3000
5 Replies

8. UNIX for Dummies Questions & Answers

ls with wildcards

ok, I'm trying to write a script file that lists files with specific elements in the name into a txt file, it looks like this ls s*.dat > file_names.txt can't figure out whats wrong with that line, any ideas? thanks in advance (10 Replies)
Discussion started by: benu302000
10 Replies

9. UNIX for Dummies Questions & Answers

wildcards

when writing a shell script (bourne) and using a unix command like 'ls' is there anything special you need to do to use a wildcard (like *)? (3 Replies)
Discussion started by: benu302000
3 Replies

10. UNIX for Dummies Questions & Answers

Wildcards in VI

I'm trying to delete lines from a large text file using VI. Every line that I am wanting to delete start with 'S' - all others do not. (A list of users) I've tried using * but doesn't seem to like it...any ideas... Doesn't have to be VI - but I'm better with VI than sed/awk. (8 Replies)
Discussion started by: peter.herlihy
8 Replies
Login or Register to Ask a Question