0

How can I delete the characters before "/", including the "/", in a string using Perl or sed?

For instance, this:

ad9a91/FFFF0000

would turn into

FFFF0000
APerson
  • 8,140
  • 8
  • 35
  • 49
user3383729
  • 27
  • 1
  • 5

4 Answers4

2

Sed solution

sed 's|[^/]*/||' file

Will remove everything up to and including the first /

or

sed 's|.*/||' file

Will remove everything up to and including the last / .

I added both as the question was not entirely clear on what the format of the string would be every time.

Awk

awk -F/ '{$0=$NF}1' file

This replaces the entire line with whatever is after the last /

  • You should explain the difference between the first and second version (the second version replaces all until the last slash, if there are several occurences of a slash in the line) – Casimir et Hippolyte Sep 24 '14 at 17:49
  • It's based on your sample (where no more word or char than what you show). first "remove" first char until first `/` and second like you write "remove" until last one. In this case it is the same effect, not if there are surround word/char but in this case both are false (but out of context also). – NeronLeVelu Sep 25 '14 at 05:50
  • 1
    @CasimiretHippolyte Added explanations :) –  Sep 25 '14 at 07:21
  • thanks,also can u please tell me a sed command which deletes last 3 lines of a file – user3383729 Sep 25 '14 at 07:29
  • @user3383729 Post a new question or use the search function and you would find an answer. A quick search revealed http://stackoverflow.com/questions/13380607/how-to-use-sed-to-remove-last-n-lines-of-a-file –  Sep 25 '14 at 07:40
1

You can use substitution,

my $str = "ad9a91/FFFF0000";
$str =~ s|^.+?/||;

or regex capture,

$str = $1 if $str =~ m|/(.+)|s;
mpapec
  • 50,217
  • 8
  • 67
  • 127
0

Use substitution for this:

my $string = "ad9a91/FFFF0000";
$string =~ s|\.+/||;
Jens
  • 67,715
  • 15
  • 98
  • 113
0
my $string = qq(sjdflksdjfsdj ad9a91/FFFF0000 slodjfsdf s);
$string =~ s{\b(\s).*?\b/}{$1}ig;
print $string;exit;

you can also try this split with any space or any tag.

depsai
  • 405
  • 2
  • 14