bash substitute first character in every line

bash, sed

Solution

There is whitespace character at the begging of each line.

you can try:

sed 's/^\s*./0/g' file

\s - match white space characters

output:

0 chrX_73833098_73834098
0 chrX_73889652_73890652
0 chrX_91194501_91195501
0 chrX_92000157_92001157
0 chrX_92106500_92107500

if you want to preserve whitespace characters:

sed 's/^\(\s*\)\(1\)/\10/g' file

I also have replaced here . with 1

Problem

My file looks like this: ``` 1 chrX_73833098_73834098 1 chrX_73889652_73890652 1 chrX_91194501_91195501 1 chrX_92000157_92001157 1 chrX_92106500_92107500 ``` I want to replace first character "1" into 0. Wanted output is : ``` 0 chrX_73833098_73834098 0 chrX_73889652_73890652 0 chrX_91194501_91195501 0 chrX_92000157_92001157 0 chrX_92106500_92107500 ``` Trying to do it with this: ``` sed 's/^./0/g' file ``` But the output is: ``` 0 1 chrX_73833098_73834098 0 1 chrX_73889652_73890652 0 1 chrX_91194501_91195501 0 1 chrX_92000157_92001157 0 1 chrX_92106500_92107500 ``` I believe there is easy way to fix it, but I don't know it.

Original source