Creating single pattern for matching multiple files.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating single pattern for matching multiple files.
# 1  
Old 04-18-2013
Creating single pattern for matching multiple files.

Hi friends,

I have a some files in a directory. for example

Code:
856-abc
856-def
851-abc
945-def
956-abc
852-abc

i want to display only those files whose name starts with 856* 945* and 851* using a single pattern.
i.e
Code:
856-abc
856-def
851-abc
945-def

the rest of the two files should not be displayed.
i.e.
Code:
956-abc
852-abc

so that if i enter a command like

Code:
ls -l $pattern

all the files matching the pattern should be displayed. can anyone write a pattern for these 3 files?

thanks
# 2  
Old 04-18-2013
Code:
$ pattern="856* 945* 851*"
$ ls $pattern
851-abc  856-abc  856-def  945-def

This User Gave Thanks to hanson44 For This Post:
# 3  
Old 04-18-2013
You can also use extended globbing as described by the bash man page:
Code:
  ?(pattern-list)   Matches zero or one occurrence of the given patterns
  *(pattern-list)   Matches zero or more occurrences of the given patterns
  +(pattern-list)   Matches one or more occurrences of the given patterns
  @(pattern-list)   Matches one of the given patterns
  !(pattern-list)   Matches anything except one of the given patterns

First of all, make sure that you enable them before using:
Code:
shopt -s extglob

Then use it with ls command:
Code:
ls @(856|945|851)*
851-abc  856-abc  856-def  945-def

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Group Multiple Lines on SINGLE line matching pattern

Hi Guys, I am trying to format my csv file. When I spool the file using sqlplus the single row output is wrapped on three lines. Somehow I managed to format that file and finally i am trying to make the multiple line on single line. The below command is working fine but I need to pass the... (3 Replies)
Discussion started by: RJSKR28
3 Replies

2. Shell Programming and Scripting

Performance assessment of using single or combined pattern matching

Hi, I want to know which pattern matching technique will be giving better performance and quick result. I will be having the patterns in a file and want to read that patterns and search through a whole file of say 70 MB size. whether if i initially create a pattern matching string while... (7 Replies)
Discussion started by: ananan
7 Replies

3. Shell Programming and Scripting

Pattern Matching and creating output

HI Unix Forum, My requirement I have two set of Patterns UBA and CIE for which different Phases are there which will have Start and End time. They are not in same order. I want the o/p in the below mentioned format. Eg: Mangolia Alien 03:04:56 Phase 0 started (10... (5 Replies)
Discussion started by: TechGyaann
5 Replies

4. Shell Programming and Scripting

sed - filter blocks between single delimiters matching a pattern

Hi! I have a file with the following format:CDR ... MSISDN=111 ... CDR ... MSISDN=xxx ... CDR ... MSISDN=xxx ... CDR ... MSISDN=111 (2 Replies)
Discussion started by: Flavius
2 Replies

5. Shell Programming and Scripting

awk script issue redirecting to multiple files after matching pattern

Hi All I am having one awk and sed requirement for the below problem. I tried multiple options in my sed or awk and right output is not coming out. Problem Description ############################################################### I am having a big file say file having repeated... (4 Replies)
Discussion started by: kshitij
4 Replies

6. Shell Programming and Scripting

Split single file into multiple files using pattern matching

I have one single shown below and I need to break each ST|850 & SE to separate file using unix script. Below example should create 3 files. We can use ST & SE to filter as these field names will remain same. Please advice with the unix code. ST|850 BEG|PO|1234 LIN|1|23 SE|4 ST|850... (3 Replies)
Discussion started by: prasadm
3 Replies

7. Shell Programming and Scripting

Creating number by pattern matching

I have a certain mnemonic string from which I want to calculate a number The pattern follows three letters s, v and d. If a letter is by its own, the number assigned to the letter is assumed to be one. Else it takes the value preceeding it. I then need to add the numbers together. Example ... (5 Replies)
Discussion started by: kristinu
5 Replies

8. Shell Programming and Scripting

SED multiple pattern matching

Hello sorry for the probably simple question - searching about the forums and Internet, I have not found the answer. Could you tell me please how to do a multiple pattern match with SED So it would be SED searching for "PATTERN1" 'or' "PATTERN2" not 'and' if they happen to fall on the same... (4 Replies)
Discussion started by: lostincashe
4 Replies

9. Programming

creating multiple threads using single thread id

Hi all, Can I create multiple threads using single thread_id like pthread_t thread_id; pthread_create(&thread_id, NULL, &print_xs, NULL); pthread_create(&thread_id, NULL, &print_ys, NULL); pthread_create(&thread_id, NULL, &print_zs, NULL); pthread_join(thread_id, NULL); what... (2 Replies)
Discussion started by: zing_foru
2 Replies

10. Shell Programming and Scripting

pattern matching over multiple lines and deleting the first

I've got a longish log file with content such as Uplink traffic: Downlink traffic: I want to parse the log file and remove any line that contains the string "Uplink traffic:" at the beginning of the line, but only if the line following it beginnings with the string "Downlink traffic:" (in... (7 Replies)
Discussion started by: Yorkie99
7 Replies
Login or Register to Ask a Question