Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to replace words in file in bash

I have a file with following content:

Blekota blaboli o koblihach.
Blanka je bl...
GEwI
er

I need to replace every word starting with Bl or bl with xxxx and save it into new file. I try this, but it did not work.

while read line; 
do pokus="${line//[Bl|bl].* /xxxx}" 
echo $pokus
done < "$TEXT" > "$TEXT".new

Desired output is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

xxxx xxxx o koblihach. 
xxxx je xxxx...
GEwI
er

What do I do wrong, please?

>Solution :

This can be done using a sed command:

sed 's/\<[Bb]l[[:alpha:]]*/xxxx/g' file

xxxx xxxx o koblihach.
xxxx je xxxx...
GEwI
er

Here \<[Bb]l[[:alpha:]]* matches a word starting with Bl or bl followed by 0 or more alphabets.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading