Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete the first character of the line only if that line matches with the given pattern [closed]

Tags:

shell

sed

awk

I need to delete first character at each line only if the given pattern matches with that line.

I have tried by replacing the first character with space, but space is appending at the each line.

 sed -e '/#/s/^/ /' 2.txt > 1.txt

I need to remove the # from each line. Kindly suggest me how to make this.

like image 612
sivakumar V Avatar asked Oct 21 '25 05:10

sivakumar V


2 Answers

You need to add a dot after ^, so that it would match the first character.

sed -e '/#/s/^./ /' 2.txt > 1.txt

Example:

$ cat file
foo
foo bar #
$ sed -e '/#/s/^./ /' file
foo
 oo bar #

If you want to remove the starting # symbol then you don't need to use a search pattern,

sed 's/^#//g' file1 > file2
like image 51
Avinash Raj Avatar answered Oct 24 '25 10:10

Avinash Raj


You can use this:

sed 's/^#//' 2.txt > 1.txt

It replaces the # at the beginning of a line by an empty string, if there is no # at the beginning of the line, then it leaves that line untouched.

like image 30
hek2mgl Avatar answered Oct 24 '25 09:10

hek2mgl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!