LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using Delimiters to get the end of the string (https://www.linuxquestions.org/questions/programming-9/using-delimiters-to-get-the-end-of-the-string-607078/)

champak 12-16-2007 04:04 AM

Using Delimiters to get the end of the string
 
Hi..

Im want to extract a part of a word. Say for example, in
abcd_def_fgh i want abcd_def. That is except the text occuring after the the last delimiter(_).Also the length and the number of delimiters are not fixed.

Some more examples:
blah_blah_blah_blah ->blah_blah_blah
a_b_c->a_b
t_d->t

Any ideas on how this can be done?

Thanks.

ghostdog74 12-16-2007 04:23 AM

in bash
Code:

# echo $s
blah_blah_blah_blah_set
# echo ${s%_*}
blah_blah_blah_blah


champak 12-16-2007 06:01 AM

Thanks man

matthewg42 12-16-2007 06:15 AM

In Perl:
Code:

my $s = "blah_blah_blah_blah_set";
$s =~ s/_[^_]*$//;
print "$s\n";



All times are GMT -5. The time now is 09:04 AM.