[SOLVED] Simple bash string manipulation - well it should be
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Simple bash string manipulation - well it should be
I have a file name, abcdefgh.mp4 and I want to just get the last four characters of the name "efgh" (okay - I know files in linux don't have extensions like in Windoze it's alll just part of the name)
I can do it with an intermediate variable, but I want one neat bit of bash:
Cheers, I should have been more explicit, I meant one command with no intermediate variable
If you are looking at passing the filename to your command instead of hard coding it (as in a script) then you will need to use the default bash command line internal variables $0, $1, etc. anyway. One of the major strengths of bash is that it allows the use of variables either on the command line or in scripts.
With that said,
Code:
ls *.mp4 | sed -n s/.mp4$//p | grep -o ....$
will give you the last 4 characters of every filename ending with .mp4 after stripping the "extension" off it. Without using a variable as asked.
Last edited by computersavvy; 11-12-2020 at 07:55 PM.
If you are looking at passing the filename to your command instead of hard coding it (as in a script) then you will need to use the default bash command line internal variables $0, $1, etc. anyway. One of the major strengths of bash is that it allows the use of variables either on the command line or in scripts.
With that said,
Code:
ls *.mp4 | sed -n s/.mp4$//p | grep -o ....$
will give you the last 4 characters of every filename ending with .mp4 after stripping the "extension" off it. Without using a variable as asked.
Another fine solution, cheers
I like examples because I go over them to see how they work and this helps me write and configure my own versions to suit.
backtick, basename, sed, grep are all new processes, they cost much more (in resources, like time, cpu, ram) than the built-in bash alternatives.
So why do you want a oneliner?
backtick, basename, sed, grep are all new processes, they cost much more (in resources, like time, cpu, ram) than the built-in bash alternatives.
So why do you want a oneliner?
Code:
echo ${ff: -8: -4}
I don't know why, but GPGAgent specifically asked for no variable in the one-liner. The only way I know to do that requires chaining processes together instead of using the built-in variable tools of bash. Yours works cleanly and efficiently but does require a variable be used.
I don't know why, but GPGAgent specifically asked for no variable in the one-liner. The only way I know to do that requires chaining processes together instead of using the built-in variable tools of bash. Yours works cleanly and efficiently but does require a variable be used.
When I said a one liner, I really meant no intermediate variables or even piping temp results to another process, and this solution is what i was looking for but couldn't get my head around the double negative as it were:
That's correct, left it in by mistake - cut 'n paste and didn't check it, btw I should mention the space is important and this is the alternate a\nd perhaps more accurate syntax
Code:
jonke@charlie:~$ echo ${ff:(-8)} # alternate syntax
5678.mp3
jonke@charlie:~$ echo ${ff: -8} # space before -8 is important
5678.mp3
jonke@charlie:~$ echo ${ff:-8} # no, incorrect
12345678.mp3
jonke@charlie:~$
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.