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 2nd filed of line separated by comma with a value stored in variable

i have a line as below and all fields separated by comma:

I want to replace 2nd filed which is 11:50:21.444 with a value stored in variable "b"

01-01-2022,11:50:21.444,1234543233443,0,0,0,0,1

I tried using sed
cat abc.csv| sed ‘s/$2/$b/’

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 :

Any time you find yourself talking about fields you should use awk rather than sed or grep since awk has a concept of fields while the others don’t and awk can do literal string replacements while sed can’t.

awk -v b="$b" '{$2=b; print}' abc.csv
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