Pattern match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern match
# 1  
Old 05-17-2013
Pattern match

Hi all,

i am using this code , but having some problem,
Code:
pattern=File_[0-9]*.csv
pattern1=File_[0-9]*_[0-9]*.csv

and then i am comparing the patterns like this
Code:
case ${arr[$i]} in
  $pattern) echo "pattern %N% match" ;;
  esac
case ${arr[$i]} in
  $pattern1) echo "pattern %N%_%N%  match" ;;
  esac

but if name of my file is File_23_23.csv
then it is returning
Code:
pattern %N% match
pattern %N%_%N%  match

but the desired result should be only pattern %N%_%N% match only.

can any one please help me.

Last edited by ramsavi; 05-17-2013 at 04:01 AM.. Reason: Please use code tags
# 2  
Old 05-17-2013
Quote:
Originally Posted by ramsavi
Hi all,

i am using this code , but having some problem,
Code:
pattern=File_[0-9]*.csv
pattern1=File_[0-9]*_[0-9]*.csv

and then i am comparing the patterns like this
Code:
case ${arr[$i]} in
  $pattern) echo "pattern %N% match" ;;
  esac
case ${arr[$i]} in
  $pattern) echo "pattern %N%_%N%  match" ;;
  esac

but if name of my file is File_23_23.csv
then it is returning
Code:
pattern %N% match
pattern %N%_%N%  match

but the desired result should be only pattern %N%_%N% match only.

can any one please help me.
You've got a couple of problems. First, you define pattern and pattern1, but you never use pattern1. And, second, these are filename matching patterns, not regular expressions. So the filename matching pattern File_[0-9]*.csv will match any string that starts with "File_" followed by a single digit followed by any number of any character, followed by ".csv" at the end of the string.

If you want to match a filename like File_23_23.csv you could use File_[0-9][0-9]_[0-9][0-9].csv.
# 3  
Old 05-17-2013
but i dont know the length of that i.e it can be

File_23_23.csv
or
File_112_156.csv
or
File_11111_55577.csv

or it can be any number.

and other thing
File_12_45.csv is being matched by File_[0-9]*.csv i.e underscore is also being taken in [0-9]* although it is not a numeral.
is there any way i can distinguish between these types of file like File_%N%.csv and File_%N%_%N%.csv


Thanks
# 4  
Old 05-17-2013
What shell are you using?
What OS are you using?
# 5  
Old 05-17-2013
shell is bash
os - linux
# 6  
Old 05-17-2013
The following seems to do what you want and works with both recent versions of bash and Korn shells with a version of 1993 or later:
Code:
#!/bin/bash
arr=("File_23_23.csv" "File_112_156.csv" "File_11111_55577.csv" "File_23.csv" "File_112.csv" "File_11111.csv" "File_xyz.csv")
for i in "${arr[@]}"
do      printf "Processing %s:\n" "$i"
        if [[ "$i" =~ File_[0-9]+.csv ]]
        then    echo 'Single digit string.'
        elif [[ "$i" =~ File_[0-9]+_[0-9]+.csv ]]
        then    echo 'Double digit string.'
        else    echo 'No match'
        fi
done

producing the output:
Code:
Processing File_23_23.csv:
Double digit string.
Processing File_112_156.csv:
Double digit string.
Processing File_11111_55577.csv:
Double digit string.
Processing File_23.csv:
Single digit string.
Processing File_112.csv:
Single digit string.
Processing File_11111.csv:
Single digit string.
Processing File_xyz.csv:
No match

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

2. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

3. Shell Programming and Scripting

sed : match one pattern then the next consecutive second pattern not working

Ive used this snippet of code on a solaris box thousands of times. But it isnt working on the new linux box sed -n '/interface LoopBack0/{N;/ ip address /p;}' *.conf its driving me nuts !! Is there something Im missing ? (7 Replies)
Discussion started by: popeye
7 Replies

4. Shell Programming and Scripting

Print only next pattern in a line after a pattern match

I have 2013-06-11 23:55:14 1Umexd-0004cm-IG <= user@domain.com I need sed/awk operation on this, so that it should print the very next pattern only after the the pattern mach <= ie only print user@domain.com (7 Replies)
Discussion started by: anil510
7 Replies

5. Shell Programming and Scripting

Pattern match exclusive return pattern/variable

I have an application(Minecraft Server) that generates a logfile live. Using Crontab and screen I send a 'list' command every minute. Sample Log view: 2013-06-07 19:14:37 <Willrocksyea1> hello* 2013-06-07 19:14:41 <Gromden29> hey 2013-06-07 19:14:42 Gromden29 lost connection:... (1 Reply)
Discussion started by: gatekeeper258
1 Replies

6. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

7. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

8. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

9. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

10. Shell Programming and Scripting

Match first pattern first then extract second pattern match

My input file: <accession>Q91G55</accession> <name>043L_IIV6</name> <protein> <recommendedName> <location> <position position="294"/> </location> <fullName>Uncharacterized protein 043L</fullName> <accession>P18556</accession> <name>1106L_ASFB7</name> <protein> <recommendedName>... (5 Replies)
Discussion started by: patrick87
5 Replies
Login or Register to Ask a Question