Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Grep for a particular file in a directory

Posted on 2012-04-10
5
Medium Priority
?
465 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

610 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