LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-03-2017, 10:39 AM   #1
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Rep: Reputation: Disabled
Standard wildcard and regex


There is standard wildcard (gobbling pattern) and regex.
Seems like the author said imply they are different.
See here:
http://www.tldp.org/LDP/GNU-Linux-To...tml/x11655.htm

What is the difference?

I tried these commands:
Code:
 $ ls -a
.  ..  dir  dir1  .dotfile  .dotfile1  file1  file2  file3  

# Using ? like this does not work. Why?
 $ ls f?ile?
ls: cannot access 'f?ile?': No such file or directory

# But using ? like this works
 $ ls file?
file1  file2  file3

#Using .(dot) like this does not work
#How do you indicate dot is to be relace with any character? 

 $ ls f.le?
ls: cannot access 'f.le?': No such file or directory

# Using .* does not work either.  Why?
 $ ls f.*le?
ls: cannot access 'f.*le?': No such file or directory

#But this works.
 $ ls f*le?
file1  file2  file3
Please advice.
Thank you & Happy New Year!.
 
Old 01-03-2017, 10:55 AM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 20 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918
These should help you: http://www.tldp.org/LDP/abs/html/globbingref.html and http://www.tldp.org/LDP/GNU-Linux-To...tml/x11655.htm

Note that a dot '.' is not one of the wildcard characters.

Note also that '?' represents a single character and can't represent no characters at all.
 
1 members found this post helpful.
Old 01-03-2017, 11:02 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,821

Rep: Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084
You will need to supply more information as explanations for each example you have shown are answered under the wildcards section. Perhaps the issue is that none of your examples are regular expressions ...
are you aware of this? The bash shell, by default, will expand wildcards when used in conjunction with commands, in your case ls.

So to your examples:
Code:
$ ls f?ile? -- show all files containing the character 'f' followed by any character, followed by string 'ile', followed by any character ... none exist

$ ls file?  -- show all files starting with string 'file' followed by any character ... 3 exist as '?' can be 1, 2 or 3

$ ls f.le?  -- show all files starting with string 'f.le' followed by any character ... none exist, dot is in no files starting with an 'f'

$ ls f.*le? -- show all files starting with string 'f.' followed by any number of any character, followed by string 'le' followed by any character ... none exist, an example would be .. f.asd123leN

$ ls f*le?  -- show all files containing the character 'f' followed by any number of any character, followed by string 'le' followed by any character ... 3 exist as * will be replaced by 'i' in all examples
and '?' replaced by 1 - 3
 
1 members found this post helpful.
Old 01-03-2017, 11:27 AM   #4
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
Smile

Thanks folks.

This is a typo:
Quote:
$ ls f?ile?
It should be this:
Quote:
$ ls f?le?
file1 file2 file3
I now understood.
First, bash parse the command, but it does not use regex.
Bash use it own wildcard, i.e.globbing pattern.
See here http://www.tldp.org/LDP/abs/html/globbingref.html
Excerpts:" Bash does carry out filename expansion [1] -- a process known as globbing -- but this does not use the standard RE set. "

My mistake was also this:
dot is not a globbing pattern wildcard (which is different from regular regex).

Then bash executes the command with the expanded arguments that were not escaped.
The executed command use regular regex on the arguments, if there are any that escaped Bash's parsing.

Here is blessings for everyone:
Quote:
Let peace, let peace, let peace
fill your soul.

May peace, may peace, may peace
make you whole.
 
Old 01-03-2017, 11:33 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,821

Rep: Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084
Quote:
The executed command use regular regex on the arguments, if there are any that escaped Bash's parsing.
Not sure where you are going with the above statement, it is outright wrong when looking at a command like ls, but something like grep would use regular expressions. Any escaped item would also be escaped
for the regular expression unless the command involved does not use the same escape item.

The rest of your information appears correct.
 
Old 01-03-2017, 11:47 AM   #6
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Not sure where you are going with the above statement, it is outright wrong when looking at a command like ls, but something like grep would use regular expressions. Any escaped item would also be escaped
for the regular expression unless the command involved does not use the same escape item.

The rest of your information appears correct.
Thank you for the clarification.
I should have mentioned that I was referring to quotes which prevents Bash from parsing the quoted arguments when used by commands like grep,.... etc.
Code:
$ echo f?le?
file1 file2 file3

$ echo "f?le?"
f?le?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Confusing issue with Perl regEx - Regex check seems to require variable being set EnderX Programming 1 09-07-2013 04:36 AM
[SOLVED] DNS problem with standard and wildcard record mlewis Linux - General 1 01-11-2013 01:03 PM
grep regex with wildcard yknot7 Linux - Newbie 6 11-10-2011 01:11 AM
Wildcard/regex in bash command logicalfuzz Linux - General 2 10-27-2011 02:40 PM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration