LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 08-17-2005, 09:20 AM   #1
nukey
Member
 
Registered: Dec 2004
Location: Netherlands
Distribution: Slackware
Posts: 173

Rep: Reputation: 30
can anyone help me on this one (sed)


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 ?
 
Old 08-17-2005, 10:40 AM   #2
PenguinPwrdBox
Member
 
Registered: Oct 2003
Posts: 568

Rep: Reputation: 31
Your quantifier was in the wrong place.
It should read:
Code:
sed s/-[a-z]+$//g
or, the cleaner way to do it is:
Code:
sed s/-\w*$//g

Last edited by PenguinPwrdBox; 08-17-2005 at 10:46 AM.
 
Old 08-17-2005, 10:53 AM   #3
nukey
Member
 
Registered: Dec 2004
Location: Netherlands
Distribution: Slackware
Posts: 173

Original Poster
Rep: Reputation: 30
Thanks for replying but it doesn't seem to work

$ 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.

Last edited by nukey; 08-17-2005 at 11:01 AM.
 
Old 08-17-2005, 05:55 PM   #4
nukey
Member
 
Registered: Dec 2004
Location: Netherlands
Distribution: Slackware
Posts: 173

Original Poster
Rep: Reputation: 30
More sed-experts on this forum ?
 
Old 08-18-2005, 07:33 AM   #5
nukey
Member
 
Registered: Dec 2004
Location: Netherlands
Distribution: Slackware
Posts: 173

Original Poster
Rep: Reputation: 30
Got it, not nice, but it works

sed 's/\(.*\)-.*/\1/' | sed 's/\(.*\)-.*/\1/' | sed 's/\(.*\)-.*/\1/'
 
Old 08-18-2005, 07:51 AM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Why not use cut ?
Code:
echo "random1-random2-random3-random" | cut -d'-' -f1
Maybe I don't well get it
 
Old 08-18-2005, 08:02 AM   #7
nukey
Member
 
Registered: Dec 2004
Location: Netherlands
Distribution: Slackware
Posts: 173

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by keefaz
Why not use cut ?
Code:
echo "random1-random2-random3-random" | cut -d'-' -f1
Maybe I don't well get it
cut could work, but as i said, in some lines random1 also contains a dash, so it doesn't work for lines where random1 contains a dash.
 
Old 08-18-2005, 08:04 AM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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

Last edited by keefaz; 08-18-2005 at 08:06 AM.
 
Old 08-18-2005, 08:05 AM   #9
nukey
Member
 
Registered: Dec 2004
Location: Netherlands
Distribution: Slackware
Posts: 173

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by keefaz
Is the dash at the end or at the beginning of random1 or at random position ?
ra-ndo-m1-random2-random3-random4
r-a-n-dom1-random2-random3-random4

I think you get the picture
 
Old 08-18-2005, 08:08 AM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Maybe :
Code:
echo "r-a-n-dom1-random2-random3-random4" | sed 's/\(.*\)-\(.*\)-\(.*\)-.*/\1/'
 
Old 08-18-2005, 08:10 AM   #11
nukey
Member
 
Registered: Dec 2004
Location: Netherlands
Distribution: Slackware
Posts: 173

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by keefaz
Maybe :
Code:
echo "r-a-n-dom1-random2-random3-random4" | sed 's/\(.*\)-\(.*\)-\(.*\)-.*/\1/'
Yup, thanks, way shorter
 
Old 08-18-2005, 09:34 AM   #12
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
From your spec, here's what I'd write:
Code:
sed 's/-[^-]*-[^-]*-[^-]*$//'
Yves.
 
  


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
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Sed q? ky-lab_rat Linux - Software 5 10-26-2004 07:59 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
Help with sed odious1 Linux - General 2 08-29-2003 10:52 AM
help with sed DavidPhillips Programming 3 08-11-2003 04:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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