LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-15-2003, 05:28 PM   #1
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
How to extract a part of a line by sed?


I have a variable that contains the following line of text:

someusername-sixdigitsofrandomnumbers

How can I extract someusername from this line by sed?

Naturally, someusername and the six digits of random numbers may change, only the '-' separator between them is always unchanged.

(I could do the task in a bash script without sed, but this would be used in a procmailrc file, where I do not think the same construct would work, so I'd prefer sed)

Last edited by J_Szucs; 02-15-2003 at 05:29 PM.
 
Old 02-15-2003, 05:40 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Code:
[chris@trevor chris]$ echo chris-123456 | sed s/[^a-z]//g
chris
is that what you're after? that's a very trivial regex, have an experiment to see what you can do with them.

in actual fact.... the username could contain numbers no? so assuming it won't have a - in it (no sure if it's actaully possbile of not.. guess it should be:
Code:
[chris@trevor chris]$ echo chris-123456 | sed s/-.*//g
chris
you might want to keep adding certain checks etc...
 
Old 02-15-2003, 06:49 PM   #3
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Original Poster
Rep: Reputation: 58
I choosed the second solution, and it works.
Thanks!
 
  


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 printing only part of a line jrdioko Linux - Software 12 05-17-2012 07:15 PM
sed to extract multiple matches in a line? mhoch3 Linux - Software 8 08-01-2005 03:32 PM
Command line extract Daunted Linux - Software 2 09-30-2004 05:37 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
sed: replace one line with >one line bbeers Programming 3 11-19-2002 05:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:32 AM.

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