Differences between revisions 8 and 9
Revision 8 as of 2009-06-04 20:36:27
Size: 2606
Editor: GeoHump
Comment:
Revision 9 as of 2009-06-04 20:45:06
Size: 2812
Editor: GeoHump
Comment:
Deletions are marked like this. Additions are marked like this.
Line 16: Line 16:
This article will cover how to do all of these using the Bash PE and will introduce the more powerful actions available with PE's. Pleasenote there is a BashFaq about PE's already. That FAQ covers more of the scope of PE capabilities, where this one instead focuses on string operations. This article will cover how to do all of these using the Bash PE and will introduce the more powerful actions available with PE's. Please note there is a BashFaq about PE's already. That FAQ covers more of the scope of PE capabilities, where this one instead focuses on string operations.
Line 20: Line 20:
to perform a basename in bash we use the following PE expression: ${fullpath##*/} which returns "Its_only_Rock_and_Roll.mp3". To get a basename in bash we use the PE expression: ${fullpath##*/} which returns "Its_only_Rock_and_Roll.mp3".
Line 22: Line 22:
to perform a dirname in bash we use the following PE expression: ${fullpath%/*} which produces "usr/home/JosephBaldwin". To perform a dirname in bash we use the PE expression: ${fullpath%/*} which produces "usr/home/JosephBaldwin".
Line 24: Line 24:
to perform a dropext in bash we use the following PE expression: ${fullpath%.*} giving out "usr/home/JosephBaldwin/Its_only_Rock_and_Roll" To perform a dropext in bash we use the PE expression: ${fullpath%.*} giving out "usr/home/JosephBaldwin/Its_only_Rock_and_Roll"
Line 26: Line 26:
to perform a getext in bash we use the following PE expression: ${fullpath##*.} generating only "mp3". To get the fielname's extension we use the PE expression: ${fullpath##*.} generating only "mp3".
Line 28: Line 28:
to perform a strlen in bash we use the following PE expression: ${#fullpath} which is
 49.
to perform a leftstr in bash we use the following PE expression: ${fullpath:0:$2}
To find the strlen the PE expression: ${#fullpath} finds it and its 49.
Line 32: Line 30:
to perform a rightstr in bash we use the following PE expression: ${fullpath:$(( 0 - $2 ))} To perform a leftstr in bash we use the PE expression: ${fullpath:0:20} grabbing the first 20 chars of fullpath to make "usr/home/JosephBaldw".usr/home/JosephBaldwin/Its_only_Roll_and_Roll.mp3
Line 34: Line 32:
to perform a midstr  in bash we use the following PE expression: ${fullpath:$2:$3} To perform a rightstr in bash we use the following PE expression: ${fullpath:$(( 0 - 20 ))} which gest the last 20 chars, "ly_Rock_and_Roll.mp3".
Line 36: Line 34:
to perform a substr in bash we use the following PE expression: ${fullpath//$2/$3} To perform a midstr  in bash we use the following PE expression: ${fullpath:10:20} making "osephBaldwin/Its_onl"
Line 38: Line 36:

To perform a substr in bash we use the following PE expression: ${fullpath//Rock/Roll} rolling it into "usr/home/JosephBaldwin/Its_only_Roll_and_Roll.mp3".

Bash can do string operations. LOTS of string operations. This is an introduction to bash string operations for those new to Bash's special tool/feature called "Parameter Expansion", (PE), with a focus on typical string operations. Note Bash's Parameter Expansion, (PE), capability is a lot more powerful than the typical string manipulation calls you may be used to. There are some twists in the road up ahead.

Here is a list of some typical string manipulation functions/subroutines that you may already be familiar with:

  • strlen returns the length of the string
  • leftstr returns a string N chars long starting from the left hand side
  • rightstr returns a string N chars long starting from the right hand side
  • midstr returns a string N chars long starting from offset K chars from the beginning/end
  • substr returns copy of the string with all instances matching a patter replaced with a new string
  • basename returns the last component of a pathname (everything after the last "/")
  • dirname returns everything in the pathname up to, but not including the last "/"
  • getext returns a filenames extension
  • dropext returns the filename without its extension.

This article will cover how to do all of these using the Bash PE and will introduce the more powerful actions available with PE's. Please note there is a BashFaq about PE's already. That FAQ covers more of the scope of PE capabilities, where this one instead focuses on string operations.

lets assume we have a string variable named fullpath whose value is "usr/home/JosephBaldwin/Its_only_Rock_and_Roll.mp3"

To get a basename in bash we use the PE expression: ${fullpath##*/} which returns "Its_only_Rock_and_Roll.mp3".

To perform a dirname in bash we use the PE expression: ${fullpath%/*} which produces "usr/home/JosephBaldwin".

To perform a dropext in bash we use the PE expression: ${fullpath%.*} giving out "usr/home/JosephBaldwin/Its_only_Rock_and_Roll"

To get the fielname's extension we use the PE expression: ${fullpath##*.} generating only "mp3".

To find the strlen the PE expression: ${#fullpath} finds it and its 49.

To perform a leftstr in bash we use the PE expression: ${fullpath:0:20} grabbing the first 20 chars of fullpath to make "usr/home/JosephBaldw".usr/home/JosephBaldwin/Its_only_Roll_and_Roll.mp3

To perform a rightstr in bash we use the following PE expression: ${fullpath:$(( 0 - 20 ))} which gest the last 20 chars, "ly_Rock_and_Roll.mp3".

To perform a midstr in bash we use the following PE expression: ${fullpath:10:20} making "osephBaldwin/Its_onl"

To perform a substr in bash we use the following PE expression: ${fullpath//Rock/Roll} rolling it into "usr/home/JosephBaldwin/Its_only_Roll_and_Roll.mp3".


CategoryShell

BashFAQ/100 (last edited 2023-06-26 10:03:19 by StephaneChazelas)