grep for pattern in filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep for pattern in filename
# 1  
Old 10-01-2011
grep for pattern in filename

Hey guys,

here is my code:

Code:
#!/bin/bash

 filter=('ubb' 'um2' 'uuu' 'uvv' 'uw1' 'uw2' 'uwh')
 let num=`ls -l | grep 'sk' | wc -l`
 read -a lines <<< `ls -l | grep 'sk' | awk '{print $8}'`

 let finum=${#fi[@]}

 for ((i=1;i<=$num;i++))

    do

        for ((c=4;c<6;c++))

            do
                
            for ((q=0;q<finum;q++)); do

            grep -q "${filter[$q]}" "${lines[$i-1]}" && fitemp="${filter[$q]}" || fitemp=0
            echo ${filter[$q]} ${lines[$i-1]}
            echo $c : $fitemp
            echo ''
                done

            #uvotmaghist ${lines[$i-1]} $fitemp$c.fits $fitemp$c.gif CALDB CALDB CALDB src$c.reg bkg$c.reg

            done

    done

There is a file (sw000uw2_sk.img.gz) in the directory.
With the grep line I would like to choose that filter, which can be found in the filename (so the uw2 filter),
and then use it, but it gives accordance with the filters um2, uvv, uw1 as well.
I've tried other versions for that line, but I get the same results.
What do I miss?


Thanks for the help,
Joe
This User Gave Thanks to jkobori For This Post:
# 2  
Old 10-01-2011
Code:
...
 let finum=${#filter[@]}
...

that may be the issue... I didnt go thru the code though...

--ahamed
# 3  
Old 10-01-2011
Thanks, ahamed, but I just mistyped it.
However, if I write the filters this way:
Code:
filter=('ubb_sk' 'um2_sk' 'uuu_sk' 'uvv_sk' 'uw1_sk' 'uw2_sk' 'uwh_sk')

it works!
Anyway, it is still a mystery, why the previous version
does not work.

Joe
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match filename pattern with -f

Hello All, I have two issues. 1).I want to check if directory exists and inside that if file exists with today's date minus one. I can check directory exists but how can i check only a pattern of filename in that directory.Name of file is files-20170105-09.gz. 2).Also i want to exit immediately... (6 Replies)
Discussion started by: looney
6 Replies

2. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

3. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

4. Shell Programming and Scripting

Diff between grep .* file name and grep '.*' filename

Hi, Can anyone let me know what is difference between grep .* foo.c grep '.*' foo.c I am not able to understand what is exact difference. Thanks in advance (2 Replies)
Discussion started by: SasDutta
2 Replies

5. Shell Programming and Scripting

sed to replace pattern with filename

Hi all, I'm trying to replace a pattern/string in about 100 files with the filename using following commands but getting nowhere: for f in *.fa; do sed "s/^>.*/>$f/g" $f > $f_v1.fa; done for f in *.fa; do sed 's/^>.*/>`echo $f`/' > $fa_v1.fa; done Basically I want to change any line... (5 Replies)
Discussion started by: ivpz
5 Replies

6. UNIX for Dummies Questions & Answers

Help with filename pattern matching

Hi, I have files in a directory with filenames that match three specific patterns: 1) *'.L2_LAC'* 2) *'.L2_LAC_OC'* 3) *'.L2_LAC_SST'* I would like to have an "ls" command that will list only files matching the first two patterns. However, if I type: ls *'.L2_LAC'* I will get files that... (2 Replies)
Discussion started by: msb65
2 Replies

7. UNIX for Advanced & Expert Users

fgrep - printing pattern and filename

Hi, I have a patternfile with following pattern cat dog cow pig Let's say I have thousand files file0001 file0002 file0003 . . . file1000 Each pattern can occur multiple times in multiple files. How can I search for pattern so the output of pattern and the filename is printed... (1 Reply)
Discussion started by: yoda9691
1 Replies

8. Shell Programming and Scripting

Filename pattern count

Hi I have the following files in a directory - ds_pl_W_Test ds_pl_list_Test ds_pl_list_Test ds_pl_listl_Test ds_pl_file_Test ds_pl_xx_Test I would like to count the number files in the directory that contain the word "list" I tried the following but it returns zero - ... (3 Replies)
Discussion started by: Prega
3 Replies

9. Shell Programming and Scripting

Does Filename Match Pattern

Hi, I am writing a BASH script. I have a list of files and I would like to make sure that each is of a specific pattern (ie *.L2). If not I would like to remove that file. How do I test whether a filename matches a given pattern? Thanks a lot. Mike (10 Replies)
Discussion started by: msb65
10 Replies

10. Shell Programming and Scripting

Filename and Pattern matching

Hiiiii every one, I am facing a problem while giving a file name which has space in it. The problem is ... I have to read a file where the set of input files are stored. Now getting that name I have to open the file and i have to extract the name of the user from where it is written like"by... (2 Replies)
Discussion started by: kheyali Mitra
2 Replies
Login or Register to Ask a Question