?
Solved

Grep for a particular file in a directory

Posted on 2012-04-10
5
Medium Priority
?
463 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
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…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

771 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