Solved

Grep for a particular file in a directory

Posted on 2012-04-10
5
458 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
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 how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…

760 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

19 Experts available now in Live!

Get 1:1 Help Now