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

Insert comma before a specific word in .txt in bash

I have text file and it has content like this,

40 number of cpu
50 number of errors

and I need to insert comma between number and words. Its should be like this,

 40, number of cpu
 50, number of errors

I’m really new to linux and I tried some sed and awk commands, but was uneble to found solution.

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

>Solution :

Put comma after the first word in a line:

sed -E 's/^[[:space:]]*[^[:space:]]+/&,/' file.txt

Put comma after the first word in a line, only if it’s numeric:

sed -E 's/(^[[:space:]]*[0-9]+)([[:space:]]|$)/\1,\2/' file.txt

Put comma after every numeric field:

sed -E 's/(^|[[:space:]])([[:digit:]]+)($|[[:space:]])/\1\2,\3/g' file.txt
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