sed command to replace slash in date format only

Hello experts.

I haven't been able to find a solution for this using the sed command.

I only want to replace the forward slash with string "FW_SLASH" only if there's a number right after the slash while preserving the original number.

I have a file containing 2 entries:

Original File:
/data/usr/one.txt
/01/02/2001
Desired File:
/data/usr/one.txt
FW_SLASH01FW_SLASH02FW_SLASH2001

I have tried this code but I'm not able to preserve the original number:

sed 's/\/[0-9]/FWD_SLASH/g'

Thanks.

sed 's|/\([0-9][0-9]*\)|FW_SLASH\1|g' infile
1 Like

Or more short:

sed 's/\//FWD_SLASH/g' infile

Regards.

That would substitute the no-digit line, too.

It's true, I'm attached specific case and not general :o
but, in this case:

sed 's|/\([0-9]\+\)|FW_SLASH\1|g' infile

work as verdepollo version, but both not work with example /0FOO/2BAR/2001

Regards.

Some sed versions support word-boundary anchors \< and \>

$ echo /0FOO/2BAR/2001 | sed 's|/\([0-9][0-9]*\)|FW_SLASH\1|g'
FW_SLASH0FOOFW_SLASH2BARFW_SLASH2001
$ echo /0FOO/2BAR/2001 | sed 's|/\([0-9][0-9]*\>\)|FW_SLASH\1|g'
/0FOO/2BARFW_SLASH2001