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.
for i in *.xxx;
do name=`echo "$i" | cut -d'.' -f1
This script segment preserves everything up to the first '.' and cuts the rest. It fails when there's more than 1 dot in the filename.
That, however is the wrong way around for me, as some files have numerous dots. How would I cut from the other end? For example, how would I cut the '.xxx' fromn a file like 'long.silly.file.name.xxx'
At first, this title is meaningless, would be nice to use a meaningful one. See man bash and parameter expansion, especially ${var##.*} and ${var%*} and friends https://www.gnu.org/software/bash/ma...eter-Expansion
I second MadeInGermany. There is no reason to work in a shell unless you plan to use the power of the shell. If your shell is BASH, you have some powerful built-in string processing, so use it.
(Same can be said of KSH, ZSH, and some of the other shells but they may offer different options and implemented differently.)
If you know the extension, as seems you want to strip only that part, then you could also use the basename command to get everything but the extension part.
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803
Rep:
Quote:
Originally Posted by business_kid
Code:
for i in *.xxx;
do name=`echo "$i" | cut -d'.' -f1
This script segment preserves everything up to the first '.' and cuts the rest. It fails when there's more than 1 dot in the filename.
That, however is the wrong way around for me, as some files have numerous dots. How would I cut from the other end? For example, how would I cut the '.xxx' fromn a file like 'long.silly.file.name.xxx'
Wow! First time I looked at this there was 10 replies. Thanks guys. And sorry, there was a word missing out of the subject.
Thanks to all, and kudos to MadeInGermany who was first in with a very elegant solution which has just been implemented and has completed it's first working run.
Along with all the half baked & useless tripe they had modules on in my hardware degree, you'd think they'd have time for an essential skill like scripting.
It should be noted (by the OP) that bash (note shell specific) features are continually added. They likely didn't exist when you were a squeaky voiced student.
It should be noted (by the OP) that bash (note shell specific) features are continually added. They likely didn't exist when you were a squeaky voiced student.
Some call it bloat ... :shrug:
When I was a student BASH had not been written yet. Those man pages are worth gold, when they are well written and current. Search engines and the people who have put examples of new features and using them properly on the internet: priceless!
It should be noted (by the OP) that bash (note shell specific) features are continually added. They likely didn't exist when you were a squeaky voiced student.
Some call it bloat ... :shrug:
No squeaky voice as a student here.
I didn't get my degree until my kids were grown - I was turning into an old fart
Every job offered for me after I closed my business needed a degree, so I decided to get one. What with my wife getting sick, and getting a year chopped out of my degree midstream(!) it proved to be a forgettable experience. In fact, the lecturers learned nearly as much from me as I did from them.
I graduated with a degree more suited to the last millenium in 2014, and had a stroke the next year .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.