Regular expression – What is the best way to make a string operation in a shell script?

I have a path as a string in a shell script, which can be absolute or relative:

/usr/userName/config.cfg< /p>

or

../config.cfg

I want to extract the file name (the part after the last one, so in this case: “config. cfg”)

I think the best way is to use some simple regular expressions?
Is it correct? Should I or should I use sed or awk?

The string manipulation function of the Shell script itself looks very original and very esoteric.

Any sample solutions are also appreciated.

I just use instead of string manipulation

file=` basename "$filename"`

Edit:

Thanks for relaxing some new syntax (assuming your file name is saved in $filename):

file=$(basename $filename)

I have a path as a string in a shell script, which can be absolute or relative:

/usr/userName/config.cfg

or

../config.cfg

I want to extract the file name ( After the last one, so in this case: “config.cfg”)

I think the best way is to use some simple regular expressions?
Is it correct? Should I or should I use sed or awk?

The string manipulation function of the Shell script itself looks very original and very esoteric.

Any sample solutions are also appreciated.

I just use instead of string manipulation

file=`basename "$filename"`

Edit:

Thanks for relaxing some new syntax (assuming your file name is saved in $filename):

file=$(basename $filename )

Leave a Comment

Your email address will not be published.