LinuxQuestions.org
Help answer threads with 0 replies.
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 10-04-2017, 04:53 PM   #1
damiant
LQ Newbie
 
Registered: Oct 2017
Posts: 7

Rep: Reputation: Disabled
Unhappy list query for multiple rules


hey all
simple question for you guys but i have spent a good while trying to figure it out
i have an assignment question that i am struggling with.
is it possible with a list query to list all the files that begin with two letters, have an e in the name and end with 1 or more letter

what i have so far is
ls [a-z][a-z]*e**[a-z]
this seems to work except if the filename starts with an e its not showing it in the list

Thanks in advance
 
Old 10-05-2017, 10:11 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,615
Blog Entries: 3

Rep: Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871
The program ls is getting its input from you via the shell. The shell uses globbing not regular expressions. See "man 7 glob"

You might take a look at find instead if you really need regular expressions. In particular, take a look at the -regex option. find can deal with more than one type of regular expression dialect.

Code:
man find
find -regextype help
man 7 regex
 
Old 10-05-2017, 11:10 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,501

Rep: Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110
Welcome to LQ.

Since this is an assignment I would assume that using find is not an option.

If you have not already figured the answer out were you given a list of test file names? If not what were you using?

[...] is a valid glob expression.
 
Old 10-05-2017, 03:27 PM   #4
damiant
LQ Newbie
 
Registered: Oct 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
thanks for the replies
i wasnt given a list of filenames, i was told to create them to test the command is working.
I believe I can only use the ls command.
I am also trying to figure out how to add queries using ls
for example how to say the filename contains the letter e AND the first 2 characters are letters.
So far it seems this is not working for me.

thanks again
 
Old 10-05-2017, 03:47 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,501

Rep: Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110
What have you tried and what are your test file names?
 
Old 10-05-2017, 04:00 PM   #6
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,634

Rep: Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202
Quote:
is it possible with a list query to list all the files that begin with two letters, have an e in the name and end with 1 or more letter
Yes.
The bash man page details this under "Pathname Expansion".

What you have so far only considers lower case filenames. Also, you may want to consider the -d option to ls, to avoid recursion into subdirectories.
 
Old 10-05-2017, 04:01 PM   #7
damiant
LQ Newbie
 
Registered: Oct 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
What have you tried and what are your test file names?
So, exact question
Create the command that will do the following: List all the files that begin with two letters, have an e in the name and end with 1 or more letter.
I am now trying this
ls [a-z][a-z]*e**[!0-9]

I have created about 10 files. some with no extension and some with .txt
file names = apple cake bake hardest hardest1 apple1 bakes cakes echo
Where I have got to
my command will list the files but echo will not be listed even though it starts with 2 letters and contains an e
also, it doesnt work for the files that have the .txt extensions
 
Old 10-05-2017, 05:16 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,501

Rep: Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110
Code:
[a-z][a-z]*e**[!0-9]
Because of the ...*e*... Your pattern will only match filenames where an e exists in then middle which is why echo does not work. I don't know why the .txt extensions do not match without seeing the actual filename.

Not sure what you have learned in class but we are not just going to give you the answer.

Hint:
https://www.gnu.org/software/bash/ma...-Matching.html
 
Old 10-06-2017, 03:52 AM   #9
damiant
LQ Newbie
 
Registered: Oct 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Code:
[a-z][a-z]*e**[!0-9]
Because of the ...*e*... Your pattern will only match filenames where an e exists in then middle which is why echo does not work. I don't know why the .txt extensions do not match without seeing the actual filename.

Not sure what you have learned in class but we are not just going to give you the answer.

Hint:
https://www.gnu.org/software/bash/ma...-Matching.html
All I am looking for is a pointer, i am not expecting the answer. This assignment was after first class. I am wondering if the answer can be achieved with a single ls command ?
What I cant see is how to use an "and" command or if thats even possible as I am struggling to work out how to ask for a file that starts with something, ends with something and also has a specific character somewhere in it....
 
Old 10-06-2017, 04:17 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,501

Rep: Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110
Did you read the link I posted?

Quote:
If the extglob shell option is enabled using the shopt builtin, several extended pattern matching operators are recognized. In the following description, a pattern-list is a list of one or more patterns separated by a ‘|’. Composite patterns may be formed using one or more of the following sub-patterns:
 
Old 10-06-2017, 04:36 AM   #11
damiant
LQ Newbie
 
Registered: Oct 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Did you read the link I posted?
indeed i did. I did one approx 2 hour class in linux so most of this stuff makes absolutely no sense to me "If the extglob shell option is enabled using the shopt builtin"
At this point I am going to quit while I am behind.

thanks for taking the time to reply
 
Old 10-06-2017, 04:52 AM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,501

Rep: Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110
Here is one example of extended globbing.

ls ?(e*|*e*)
 
Old 10-06-2017, 06:13 AM   #13
damiant
LQ Newbie
 
Registered: Oct 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Here is one example of extended globbing.

ls ?(e*|*e*)
-

so slightly change the command to help with the explanation

ls ?(e*|*a*) - this command will list all file names that start with an "e" and filenames that have an a anywhere which makes sense.

I have also read through here - http://www.linuxjournal.com/content/...ended-globbing but cannot get the + to work as outlined on that page.
 
Old 10-06-2017, 09:48 AM   #14
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,634

Rep: Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202
Quote:
I have also read through here - http://www.linuxjournal.com/content/...ended-globbing but cannot get the + to work as outlined on that page.
From that page
Quote:
But these are not the only forms of wildcards supported by bash. The other forms are referred to as extended globbing and you must enable them before you can use them:

$ shopt -s extglob
Also mentioned in the link in post#8
Quote:
If the extglob shell option is enabled using the shopt builtin
Learn to love the documentation!
 
Old 10-06-2017, 02:24 PM   #15
damiant
LQ Newbie
 
Registered: Oct 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
another 2 hours spent on trying to figure out that command
I am not at here - ls | grep “^[a-zA-Z][a-zA-Z]^.*e.*[a-zA-Z]$”
but that will not display any filenames that have the e as the first or second character.... aaaaaaarrrrrrrggggggghhhhhhhh
 
  


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
Do you have an updated list of firewall rules for iptables? Gary Baker Slackware 6 05-23-2014 07:14 PM
List LS query by file size hammertime1983 Linux - Newbie 3 05-27-2013 02:38 AM
iptables --list does not show pre or postrouting rules thllgo Linux - Software 2 01-28-2010 10:25 AM
HELP/QUERY: Knoppix rules woes :S kevingpo Linux - Distributions 1 09-07-2005 01:42 PM
iptables- how to list all rules? Noerr Linux - Networking 4 06-01-2002 04:09 AM

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

All times are GMT -5. The time now is 12:03 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