Delete the first known character in a string with sed

How does one delete the first known character in a string with sed?

For example, say I want to delete the first character (which is @) in the string "@ABCDEFG1234"

I want sed to verify whether "@" exists as the first character. If so, it should delete the character.

3 Answers

sed 's/^@\(.*\)/\1/'

^ means beginning of the string

@ your known char

(.*) the rest, captured

then captured block will be substituted to output Sorry, can't test it at the moment, but should be something like that

1

There's no need to capture and replace.

sed 's/^@//'

This replaces the character @ when it's first ^ in the string, with nothing. Thus, deleting it.

You can do this instead.

sed 's/^.//'

^ - Starting character
. - No of charactes(.. means two characters)

Example :

echo test123 | sed 's/^.//'
est123
echo test123 | sed 's/^..//'
st123
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like