Bash globbing - How to match 1 or more characters? (solved)

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

Bash globbing - How to match 1 or more characters? (solved)

#1 Post by disciple »

1. I can match any file except one ending in [sometext].txt
e.g.

Code: Select all

for i in !(*-out).txt
 do
  ( echo "$i" ) &
 done
I am trying to match [sometext].[any extension], not specifically .txt
If I use something like this:

Code: Select all

!(*-out).*
Then it does what I want except it also matches . and .. (because * matches 0 or more characters).
Is there a way to match 1 or more characters, or specifically exclude . and ..?
Without specifying every possible character in [] like this?:

Code: Select all

!(*-out).+([a-z])
2. (solved, presented for interested readers)
I wanted to do stuff with the filename and the extension from the successful matches, and when a file had multiple extensions e.g. somefile.csv.txt I wanted to consider them all as one, not just take the last one as the extension.
Reading the docs at https://www.tldp.org/LDP/abs/html/param ... ution.html showed this is the solution:

Code: Select all

[do something with $i] >"${i%%.*}-out.${i#*.}"
The key is %% vs %, and # vs ##.
Last edited by disciple on Tue 16 Apr 2019, 08:21, edited 3 times in total.
Do you know a good gtkdialog program? Please post a link here

Classic Puppy quotes

ROOT FOREVER
GTK2 FOREVER

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

Re: Bash globbing - How do I match 1 or more characters?

#2 Post by MochiMoppel »

disciple wrote:I am trying to match [sometext].[any extension], not specifically .txt
If I understand correctly you want to exclude all *-out files that have an extension, so why not:

Code: Select all

for i in *-out
This would match all -out files with an extension (1 or more characters after period):

Code: Select all

for i in *-out.?*
This matches all non-hidden files, except -out files which have an extension:

Code: Select all

shopt -s extglob
for i in !(*-out.+(*))

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#3 Post by technosaurus »

Good advice from Mochi. If you want more flexibility, take a dabble at awk. It's part of busybox, supports real regular expressions, arrays and programming constructs. It's also pretty easy to learn if you know shell and a good stepping stone toward learning C.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

s243a
Posts: 2580
Joined: Tue 02 Sep 2014, 04:48
Contact:

#4 Post by s243a »

Just as a quick skim it seems like the original question was answered. That said you can use regular expressions in bash without using external tools like AWK. For instance see this stack overflow thread:

https://stackoverflow.com/questions/187 ... t/18710850

However, one thing I"m not sure about is which regular expression syntax this supports. For instance does it support full PERL compatible regular expression or alternatively an older form of regular expressions.

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#5 Post by MochiMoppel »

s243a wrote:However, one thing I"m not sure about is which regular expression syntax this supports.
Bash Reference Manual wrote:POSIX extended regular expression

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

Re: Bash globbing - How do I match 1 or more characters?

#6 Post by disciple »

Oh dear - I haven't got any notifications for this or any other topic in the last month. And they're not in my gmail spam folder either. Are you guys getting notifications?
MochiMoppel wrote:
disciple wrote:I am trying to match [sometext].[any extension], not specifically .txt
If I understand correctly you want to exclude all *-out files that have an extension, so why not:

Code: Select all

for i in *-out
But I also want to include all files that don't have the -out
I guess something that simple would work if you just did it in two steps (first the -out.* and then the non -out), but we want to be more clever than that.
This would match all -out files with an extension (1 or more characters after period):

Code: Select all

for i in *-out.?*
This matches all non-hidden files, except -out files which have an extension:

Code: Select all

shopt -s extglob
for i in !(*-out.+(*))
Ah. That is most of the way there, although I think it is actually more complicated than it needs to be - it looks like this works identically:

Code: Select all

 for i in !(*-out.*)
So my problem was just that I hadn't tried moving the .* inside the first set of brackets ().

I can of course include the hidden files too which `shopt dotglob`

And it does need to be used with a ?, so it doesn't exclude a file with trailing dot but no extension.
This looks good:

Code: Select all

#  shopt dotglob
dotglob         on
#  ls --all
 -out.test9   ..           .test2-out.out      '\-out - Copy.test9'   test3-out       test5-out.txt.out   test7.txt
 .            .test1-out   .test5-out.txt.out  '\-out.test9'          test4-out.out   test6               test8-out.
#  for i in !(*-out.?*); do echo $i; done
.test1-out
\-out - Copy.test9
test3-out
test6
test7.txt
test8-out.
(Note the funky filename with a \ - right now I am at work using WSL, so it shows up in windows with one of those "missing" box characters (doesn't show up if I paste it in here though)[/code]
Last edited by disciple on Fri 12 Apr 2019, 07:41, edited 3 times in total.
Do you know a good gtkdialog program? Please post a link here

Classic Puppy quotes

ROOT FOREVER
GTK2 FOREVER

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#7 Post by disciple »

s243a wrote:Just as a quick skim it seems like the original question was answered. That said you can use regular expressions in bash without using external tools like AWK. For instance see this stack overflow thread:

https://stackoverflow.com/questions/187 ... t/18710850

However, one thing I"m not sure about is which regular expression syntax this supports. For instance does it support full PERL compatible regular expression or alternatively an older form of regular expressions.
Thanks, that's a very useful thing to point out, although I tend to find regular expressions a lot more difficult to follow than globbing.
Do you know a good gtkdialog program? Please post a link here

Classic Puppy quotes

ROOT FOREVER
GTK2 FOREVER

Post Reply