Solved

Grep for a particular file in a directory

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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…

717 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