LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 06-05-2022, 01:54 PM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
Getting field name and field value with parameter expansion


I have a bash variable named flentry that consists of

Code:
File: Value
or

Code:
+ Value
Have used

Code:
fld="${flentry%[[:blank:]]*}"
and

Code:
fl="${flentry#*[[:blank:]]}"
to get the field name File: and +, whereas fl gets the value.

Looks as if bash parameter expansion uses regex patterns.

I understand that % remove shortest trailing match, whilst # removes smallest trailing match. But I am having difficulty seeing how fld and fl get the field name and field value respectively.

How exactly are fld and fl being made? Whilst * means zero or more matches, I fail to see what "${flentry#*[[:blank:]]}" tries to match because the * immediately follows #.
 
Old 06-05-2022, 02:21 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,783

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936
Code:
${string#substring}

    Deletes shortest match of $substring from front of $string.

fl="${flentry#*[[:blank:]]}"

File: Value
The # is just the syntax for delete substring. The * in *[[:blank:]] is matching any characters from the start of the string in front of a space.
 
Old 06-05-2022, 02:38 PM   #3
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
I thought the delete uses a regex for matching, so that the * would mean zero or more characters.

Last edited by Faki; 06-05-2022 at 02:50 PM.
 
Old 06-05-2022, 02:50 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,783

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936
No, it is just pattern matching.
 
Old 06-05-2022, 03:29 PM   #5
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
What does the * match exactly. Is it a wildcard representing any number of characters?
 
Old 06-05-2022, 03:42 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,783

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936
Yes, as far as I know.
 
Old 06-05-2022, 05:02 PM   #7
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Looks like there can be any sequence of characters (including none) till the first space is encountered.
 
Old 06-06-2022, 07:07 AM   #8
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,627

Rep: Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556

The answers to everything you've asked are in this thread are provided in the Bash Reference Manual.

You really should be consulting that first, and opening threads if you can't find the right section and/or don't understand the explanation (in which case provide a direct link and/or quote and explain where you're stuck).

 
1 members found this post helpful.
Old 06-06-2022, 09:32 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,037

Rep: Reputation: 7345Reputation: 7345Reputation: 7345Reputation: 7345Reputation: 7345Reputation: 7345Reputation: 7345Reputation: 7345Reputation: 7345Reputation: 7345Reputation: 7345
Quote:
Originally Posted by Faki View Post
I thought the delete uses a regex for matching, so that the * would mean zero or more characters.
Do not assume anything, that will mislead only you.
Always try your assumption.

Quote:
Originally Posted by Faki View Post
What does the * match exactly. Is it a wildcard representing any number of characters?
Just a comment:
* has a lot of different meaning depending on the tool you use (and also we have other special chars, like these: .?[]+\ just to list a few).
It is usually some kind of wildcard or a non-regular char.
We have different regex implementations, and also different kind of pattern matching engines, so you always have to check which one is actually involved.
Even bash has several different engines, like globs, pattern matching and regex.

So if unsure just read the man page and try it, but do not assume anything.
 
Old 06-06-2022, 02:28 PM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
In order to address the leftmost blank, i.e. allow further blanks in the value, it should be
Code:
fld="${flentry%%[[:blank:]]*}"
fl="${flentry#*[[:blank:]]}"
This is the shell's native pattern matching or "globbing". A * matches zero or more characters, a ? matches one character, a [ ] matches one character out of the character set.

Regular expression (RE) has some similarities but also some fundamental differences.
There are two types of RE, BRE and ERE (and PCRE is an extension to ERE).
Standard shells do not directly support RE; you need helper programs like grep, sed, awk.
The bash shell directly uses an RE in
Code:
[[ string =~ RE ]]
 
Old 06-06-2022, 05:29 PM   #11
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,627

Rep: Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556
Quote:
Originally Posted by MadeInGermany View Post
There are two types of RE, BRE and ERE (and PCRE is an extension to ERE).
That's not great wording, since there are different types of regex engine (not just different engines, but different approaches to how they're implemented), but that is not what you're referring to.

POSIX defines BRE and ERE, but GNU tools have their own extended version of BRE and ERE which have more functionality than POSIX. (I think GNU actually uses the same engine for both BRE/ERE, just with different syntax rules.)

PCRE is not an extension to ERE, it's an unrelated library aiming for - but not achieving - compatibility with Perl (another engine).
Perhaps you were thinking of Tcl, which has ARE; described as "basically EREs with some significant extensions".

Assuming we're just considering shell scripts (not all programming languages), well there's still Python which brings us to SEVEN different regex engines that might reasonably be encountered on a Linux system.

 
  


Reply

Tags
bash



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
variable expansion and pathname expansion ShadeLover Linux - General 6 04-22-2015 10:56 PM
difference between value *value and value * value PoleStar Linux - Newbie 1 11-26-2010 03:37 PM
set mysql default value to value of other field kpachopoulos Programming 1 10-11-2007 09:04 PM
Find a field number for a value in text value - BASH severian23 Programming 3 01-10-2007 09:49 AM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM

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

All times are GMT -5. The time now is 09:05 AM.

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