Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find a pattern in file entry, remove leading hash and redirect all entries to file

Tags:

sed

Looking for the syntax to find a pattern in a file and remove the leading character from only that pattern.

For example, find -16 and remove the # and save it to file.

Tried grep 12345-16 testfile2 | sed -e "s/^#//g" which works but need to capture all entries into the input file.

Example:

From this:

something   here 12345-14
something   here 12345-15
# something here 12345-16

to this:

something   here 12345-14
something   here 12345-15
something   here 12345-16

suggestions would be much appreciated.

like image 832
user3369123 Avatar asked Sep 05 '25 19:09

user3369123


1 Answers

You can do it with just sed alone.

sed '/12345-16/s/^# *//' file

You can use -i option of sed to make in-file changes. /../ in front of sed is a regex which only makes changes on lines that has that pattern. All remaining lines will not be touched and be printed out as is.

You don't need g for global here since you are only removing the leading #. I have added a pattern of ^# * which means # or # followed by spaces at the start of the line. You can create your own pattern based on the structure of your file.

like image 85
jaypal singh Avatar answered Sep 09 '25 00:09

jaypal singh



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!