Bash replace the first character in each line

My file is as follows:

1 chrX_73833098_73834098 1 chrX_73889652_73890652 1 chrX_91194501_91195501 1 chrX_92000157_92001157 1 chrX_92106500_92107500 

I want to replace the first character "1" with 0. Required output:

  0 chrX_73833098_73834098 0 chrX_73889652_73890652 0 chrX_91194501_91195501 0 chrX_92000157_92001157 0 chrX_92106500_92107500 

Trying to do this with this:

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

But the way out:

 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 that there is an easy way to fix this, but I do not know this.

+6
source share
2 answers

When flashing each line, there is a space character.

you may try:

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

\ s - space character matching

exit:

 0 chrX_73833098_73834098 0 chrX_73889652_73890652 0 chrX_91194501_91195501 0 chrX_92000157_92001157 0 chrX_92106500_92107500 

if you want to keep whitespace characters:

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

I also replaced here. from 1

+12
source

I think this is an easy way to understand. Try:

 sed 's/1/0/1' file 

exit:

  1 chrX_73833098_73834098
 1 chrX_73889652_73890652
 1 chrX_91194501_91195501
 1 chrX_92000157_92001157
 1 chrX_92106500_92107500

I want to say that 's / partten / partten / g', the last 'g' means a place on the line.
'1' means the first match, "g" means everything, also you can use "2g", which means from the second to the last.
Give it a try.

+3
source

Source: https://habr.com/ru/post/913126/


All Articles