LinuxQuestions.org
Visit Jeremy's Blog.
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 11-07-2022, 12:59 PM   #1
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Remove extension and specified number of characters from a file name


I have a file name 0001_ThisBook.mp3 and I want to extract just the name ThisBook and I've come up with this using basename, but is there a better way
Code:
 X="0001_ThisBook.mp3"
echo "$(basename ${X:5:99} .mp3)"
 
Old 11-07-2022, 01:09 PM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,628

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

There's no need for the :99 part, and equivalent to basename can be achieved in Bash itself.

See BashFAQ #73: How can I use parameter expansion? How can I get substrings? How can I get a file without its extension, or get just a file's extension? What are some good ways to do basename and dirname?

 
1 members found this post helpful.
Old 11-07-2022, 01:21 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936
I don't know if better but there are many ways.

Assuming all filenames have a _ ...
Code:
basename ${X#*_} .mp3

or

echo "$X" | awk -F"[_|.]" '{print $2}'

Last edited by michaelk; 11-07-2022 at 01:22 PM.
 
1 members found this post helpful.
Old 11-07-2022, 04:50 PM   #4
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs up

Quote:
Originally Posted by boughtonp View Post
#73 is just what I wanted - thanks
 
Old 11-08-2022, 03:37 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
Code:
X="0001_ThisBook.mp3"
# delete pattern from the right
bn=${X%.*}
# delete pattern from the left
echo "${bn#*_}"
 
Old 11-08-2022, 07:54 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
You can also use a regex:
Code:
X="0001_ThisBook.mp3"
reg='_([^.]+)'

[[ "$X" =~ $reg ]] && echo "${BASH_REMATCH[1]}"
 
  


Reply



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
Requesting terminal command that (recursively) totals size per file extension and number of files per extension triciasurfer Linux - General 8 08-06-2020 07:08 PM
Linux file extension vs Dos file Extension? manaa Linux - Newbie 6 02-12-2009 04:19 PM
How to config apache 2.0 ignore request with specified file extension? wanna13e Linux - Server 4 07-22-2007 08:20 PM
checking if a file has a specified extension in csh grinch Programming 2 05-05-2007 07:34 PM
Opinion: (name, file name, and extension) un shiza Programming 2 06-28-2005 04:23 PM

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

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