MohamedAli
MohamedAli

Reputation: 323

Bash remove a char from line

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

Upvotes: 0

Views: 124

Answers (3)

Riot
Riot

Reputation: 16696

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#:}

Upvotes: 0

mdesantis
mdesantis

Reputation: 8517

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

Resources: Bash string manipulation

Upvotes: 0

devnull
devnull

Reputation: 123458

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!

Upvotes: 2

Related Questions