LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to extract a part of a line by sed? (https://www.linuxquestions.org/questions/programming-9/how-to-extract-a-part-of-a-line-by-sed-45555/)

J_Szucs 02-15-2003 05:28 PM

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)

acid_kewpie 02-15-2003 05:40 PM

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

J_Szucs 02-15-2003 06:49 PM

I choosed the second solution, and it works.
Thanks!


All times are GMT -5. The time now is 04:01 PM.