LinuxQuestions.org
Register a domain and help support LQ
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,373

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


All times are GMT -5. The time now is 03:26 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration