Pattern matching problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern matching problem
# 1  
Old 05-19-2013
Pattern matching problem

if i have to do pattern match for file name with digit alphanumeric value like this
File_1234.csv
File_12sd45rg.csv


i am using this File_[0-9a-zA-Z]*.csv
and File_[0-9]*.csv for digit pattern match.

when i am doing pattern match for the digit then both alphanumeric match
and digit match is coming.
where only digit match should should come.
If usinng [:digit:] then it is not working.

the part of code which i am using is like this
Code:
i=0
len=${#arr[@]}
echo $len

shopt -s extglob

pattern=*[0-9]*.*
pattern1=*[0-9]*_[0-9]*.*
pattern2=*[a-zA-z0-9]*.*
pattern3=File_[0-9]*_[0-9]*.csv
pattern4=File_[a-zA-Z]*_[a-zA-Z]*.csv
pattern5=File_[a-zA-Z0-9]*_[a-zA-Z0-9]*.csv
pattern6=File_[0-9]*[a-zA-Z]*.csv

while [ $len -gt $i ]
do
  case ${arr[$i]} in
  $pattern) echo "pattern %N% match" ;;
  esac
  case ${arr[$i]} in
  $pattern1) echo "pattern %N%_%N% match" ;;
  esac
  case ${arr[$i]} in
  $pattern2) echo "pattern %x% match" ;;
  esac


Last edited by Scott; 05-19-2013 at 01:00 PM.. Reason: Code tags NOT Icode tags; Please stop posting non Redhat questions in the Redhat forum.
# 2  
Old 05-19-2013
This seems to be a slight expansion of this thread that you started a couple of days ago discussing the same topic.

In the last two days the way case statements evaluate expressions has not changed. Case statements still perform filename pattern matches; not regular expression pattern matches.
# 3  
Old 05-19-2013
i am using it as you said but when and operator is used then it is not working

Code:
Input=($( sqlplus -s rte/rtet1@him60t1 << EOF
set heading off
select FILE_NAME_DTTM FROM prd_frg_file_jrnl where FILE_KEY > $val;
EOF
))
echo $Input


i=0
len=${#arr[@]}
echo $len

for i in "${arr[@]}"
do      printf "Processing %s:\n" "$i"
        if [[ "$i" =~ [a-bA-Z0-9][0-9]+.[a-z] ]]
        then    echo '%N% FILE MASK PASS'
        elif [[ "$i" =~ [a-zA-Z0-9][0-9]+_[0-9]+.[a-z] ]]
        then    echo '%N%_%N% FILE MASK PASS'
        elif [[ "$i" =~ [a-bA-Z0-9][0-9]+.[a-z] ]] && [[ "$Input" != "no row selected" ]]
        then    echo '%SE% FILE MASK PASS'
        elif [[ "$i" =~ [a-bA-Z0-9][a-zA-Z]+.[a-z] ]]
        then    echo '%C% FILE MASK PASS'

        else    echo 'No match'
        fi

done

can you please check where i am going wrong

Last edited by Scott; 05-19-2013 at 05:26 PM.. Reason: Code tags for the last time
# 4  
Old 05-20-2013
Quote:
Originally Posted by ramsavi
i am using it as you said but when and operator is used then it is not working

Code:
Input=($( sqlplus -s rte/rtet1@him60t1 << EOF
set heading off
select FILE_NAME_DTTM FROM prd_frg_file_jrnl where FILE_KEY > $val;
EOF
))
echo $Input


i=0
len=${#arr[@]}
echo $len

for i in "${arr[@]}"
do      printf "Processing %s:\n" "$i"
        if [[ "$i" =~ [a-bA-Z0-9][0-9]+.[a-z] ]]
        then    echo '%N% FILE MASK PASS'
        elif [[ "$i" =~ [a-zA-Z0-9][0-9]+_[0-9]+.[a-z] ]]
        then    echo '%N%_%N% FILE MASK PASS'
        elif [[ "$i" =~ [a-bA-Z0-9][0-9]+.[a-z] ]] && [[ "$Input" != "no row selected" ]]
        then    echo '%SE% FILE MASK PASS'
        elif [[ "$i" =~ [a-bA-Z0-9][a-zA-Z]+.[a-z] ]]
        then    echo '%C% FILE MASK PASS'

        else    echo 'No match'
        fi

done

can you please check where i am going wrong
The regular expressions you're using in this message do not even come close to matching the patterns you had in the 1st message in this thread. Without knowing what the formats are for the filenames you're trying to match, it is impossible to guess at why you're not matching the filenames you want to match. But there are three obvious problems.
  1. What output are you seeing from the command:
    Code:
    echo $Input

    in your script.
    The way you set the array variable Input (as marked in red in your code above) will produce an array containing three elements "no", "row", and "selected" rather than a single element "no row selected" unless the sqlplus command returns a quoted string. If it is three elements instead of one, you would need to change:
    Code:
    [[ "$Input" != "no row selected" ]]

    to:
    Code:
    [[ "${Input[@]}" != "no row selected" ]]

    to get the desired result.
  2. If you have an if statement that starts with if a and a later clause in that same statement of the form elif a && b, then the then clause of the elif clause can NEVER be executed. The code I marked in Dark Orange in your code above corresponds to a. Therefore, the
    Code:
     && [[ "$Input" != "no row selected" ]]

    in your code above will never be executed.
  3. I was careless in the other thread on this topic when I suggested using ".csv" as a regular expression to match the string ".csv". The period in this regular expression will match any character (not just a period). When you know that all of your input files have a period followed by a filename extension and you want to verify that the filename you're processing has the extension .csv, it doesn't make much difference. You now seem to be looking for a single lowercase letter following a period following a bunch of other characters. The expressions you're using will match any filename that ends with three characters where the first is alphanumeric, the second is any character, and the 3rd is a lowercase alphabetic character. Since I don't know what filenames you're trying to match, I strongly suggest that you use a bracket expression [.] or a collating element expression [...] to match the period. (You could also just escape it with a backslash, but if you want to use a variable containing a string as a way to save the expressions you want to use, you have to worry about how many backslashes you need to be sure that you actually end up with exactly one backslash in the RE in [[ string =~ RE ]] after string processing and quote removal occurs.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pattern matching problem

Hi I need a bash script that can search through a text file for all lines starting with 71502FSC1206 on every line it finds starting with this I need to place a letter F at the 127 position on that line. Thanks Paul (6 Replies)
Discussion started by: firefox2k2
6 Replies

2. Shell Programming and Scripting

Pattern matching and format problem

Hi I need a bash script that can search through a text file and when it finds 'FSS1206' I need to put a Letter F 100 spaces after the second instance of FSS1206 The format is the same throughout the file I need to repeat this on every time it finds the second 'FSS1206' in the file I have... (3 Replies)
Discussion started by: firefox2k2
3 Replies

3. Programming

pl sql . pattern matching problem

hi everyone i am facing a strange problem declare v_var number(10); begin if( regexp_like('RCDORMS_MMS_*_DAR','RCDORMS_MMS_*_DAR')) then v_var:=20; dbms_output.put_line(v_var); end if; end; / please tell me what's the wrong thing in this expression.. as i am not able to get... (1 Reply)
Discussion started by: aishsimplesweet
1 Replies

4. Shell Programming and Scripting

pattern matching problem

# cat email.txt | grep -i "To:" To: <test@example.com> # cat email.txt | grep -i "Subject" Subject: Test Subject: How are you. I need to print only test@example.com from To field need to eliminate "< & >" from To field and need to print entire subject after Subject: It should be #... (7 Replies)
Discussion started by: mirfan
7 Replies

5. Shell Programming and Scripting

Problem extracting just a part of a matching pattern

Hello everyone, this is my first post so please give me a hand. I apologize for my English, I'll try to be clear with my request. I need to write a script (Bash) which finds all the variables defined in the file .h of the folder and then writes the name of the files .c where these variables are... (1 Reply)
Discussion started by: paxilpaz
1 Replies

6. Shell Programming and Scripting

problem using sed for pattern matching

if abc.sh is 192.168.1.41 then the output that i get is v5c01 my code is sed 's/192.168.1.4/v5c0/g s/192.168.1.41/acc1/g' abc.sh 2>&1 | tee abc.sh i want to find 192.168.1.4 and replace it with v5c0 and find 192.168.1.41 and replace it with acc1 and i want to do it using sed (5 Replies)
Discussion started by: lassimanji
5 Replies

7. Shell Programming and Scripting

Pattern Matching problem in UNIX

Hello All, I need help I have a problem in searching the pattern in a file let us say the file contains the below lines line 1 USING *'/FILE/FOLDER/RETURN') ................. ................. line 4 USING *'/FILE/FOLDER/6kdat1') line 5 USING... (2 Replies)
Discussion started by: maxmave
2 Replies

8. Shell Programming and Scripting

pattern matching problem

FilesToBackup='*.track* *.xml *.vm* *.gz Trace* TRACE* "*core*" *.out fcif_data_* esi_error_* *.rollback *.sed R.* APStatus_* log* *.output* send_mail* downenv* check_env* intaspurge_db_* sqlnet.log *.rpt *.html *.csv "*TSC*"' and i am using it like this- echo Moving files from $(pwd): ... (2 Replies)
Discussion started by: namishtiwari
2 Replies

9. Shell Programming and Scripting

problem with CASE pattern matching

I am using ksh on a HP Ux. I have a simple script but am having problem with the case statement:- #!/usr/bin/sh Chl=”SM.APPLE_SWIFT_DV” LoConfirm=”” case $chl in ) LoConfirm=”Using channel at Building 1” echo “test conditon1” echo $LoConfirm;; ) LoConfirm=”Using... (2 Replies)
Discussion started by: gummysweets
2 Replies

10. Shell Programming and Scripting

pattern matching problem

I have a file with the following contents; NEW 85174 MP081 /29OCT07 CNL 85986 MP098 /28OCT07 NEW 86014 MP098 /28OCT07 NEW 86051 MP097 /27OCT07 CNL 86084 MP097 /27OCT07 Now I have to retrieve all lines that start with NEW and where the next line starts with CNL and where the MP codes are... (8 Replies)
Discussion started by: rein
8 Replies
Login or Register to Ask a Question