0

I have a file withe following data

:1:aaaaa:aaa:aaa

and i want to remove the leading colon using bash to be like

1:aaaaa:aaa:aaa
1

3 Answers 3

2

You could use sed:

sed 's/^://' filename

^ denotes the start of line, so ^: would match a colon at the beginning of a line. Replace it by nothing!

0
str=':1:aaaaa:aaa:aaa'
echo ${str:1} #=> 1:aaaaa:aaa:aaa

Resources: Bash string manipulation

0

As well as sed, you may get better performance for such a simple operation on a large file by using cut:

cut myfile -d : -f 2-

You can also extract additional fields this way with other -f values.

If you want to remove the leading colon from data in a variable, such as in a loop, you can also do

myvar=":1:aaaaa:aaa:aaa"
echo ${myvar#:}

Not the answer you're looking for? Browse other questions tagged or ask your own question.