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.
I have 50 lines of text, all containing this pattern:
random1-random2-random3-random4
All the "randoms" contain letters and numbers
None of the "randoms" have the same number of characters.
I want to take away random 2 3 and 4 and the dash before random2
random1 sometimes also contains a dash, so i started with sed and working from the end of the line by using a $
sed s/-.$//g for example gets rid of the part of the line where a dash is followed by only one character. But I want any number of character after the dash removed, same goes for random2 and random3.
After a google search I noticed that you can use the + symbol to define "any number of characters, so i tried:
sed s/-[a-z][0-9]+$//g
But that didn't work
sed s/-[a-z]+$//g doesn't even remove the characters after the dash
I'm kinda lost here and missing something I guess, any thoughts ?
$ echo "random1-random2-random3-random" | sed s/-+[a-z]$//g
$ echo "random1-random2-random3-random" | sed s/-[a-z]+$//g
$ echo "random1-random2-random3-random" | sed s/-\w*$//g
all return:
random1-random2-random3-random
And i removed the number from random4 since we are only removing letters here, but every character should be removed, maybe that can be done easier.
Is the dash at the end or at the beginning of random1 or at random position ?
[edit]
Sorry, if you want to keep the dash in random1, your solution seems better than cut
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.