Solved

Grep for a particular file in a directory

Posted on 2012-04-10
5
459 Views
Last Modified: 2012-04-10
Hi,

I am finding the text files in a particular directory, which is having any one of the names
['BAH','FOR','OSA','INT','CAB','MIN','IND','NAT','APO'] and then finding teh base name of it.
My problem being I am getting all the text files in the directory, even though their names didn't match the list.

for FF in `ls $SCRIPT_DIR/*.txt | grep -i "['BAH','FOR','OSA','INT','CAB','MIN','IND','NAT','APO']"`
do
F=`basename $FF`
File=`echo $F | sed "s/\..*$//"`
done

echo $File

arquivo.txt
abcd123.txt
BAH123.txt

My shell version is :
bash --version
GNU bash, version 3.2.16(1)-release (powerpc-ibm-aix5.2.0.0)

Please help

Regards..
0
Comment
Question by:neoarwin
  • 2
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 37827110
grep -i 'BAH\|FOR\|OSA\|INT\|CAB\|MIN\|IND\|NAT\|APO'
0
 

Author Comment

by:neoarwin
ID: 37827135
@ozo I am still getting file names which are not having matching names in the list.

for example

"arq" is a result I got
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 37827206
Try "grep -E" or "egrep" and omit the backslash escapes:

grep -E -i 'BAH|FOR|OSA|INT|CAB|MIN|IND|NAT|APO'


Instead of the "sed" construct you could do:

File=${F%.*}

Since the extension is always ".txt" you can use "basename" to strip it:

File=$(basename $FF ".txt")

The "sed" stuff is then unnecessary.

(Note that I used $( ) instead of ` ` )


wmp
0
 

Author Closing Comment

by:neoarwin
ID: 37827327
That absolutely worked :)
0
 
LVL 84

Expert Comment

by:ozo
ID: 37827337
for FF in `ls $SCRIPT_DIR/*.txt | grep -i 'BAH\|FOR\|OSA\|INT\|CAB\|MIN\|IND\|NAT\|APO'`
do
F=`basename $FF`
File=`echo $F | sed "s/\..*$//"`
echo $File
done

Unless $SCRIPT_DIR matches BAH|FOR|OSA|INT|CAB|MIN|IND|NAT|APO


shopt -s nullglob extglob
for FF in $SCRIPT_DIR/*{BAH,FOR,OSA,INT,CAB,MIN,IND,NAT,APO}*.txt
do
echo ${FF//@(*[\/]|.*)}
f=${FF##*/}
File=${f%%.*}
echo $File
done
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now