5

I have one file called abc.csv, its format is:

aa,size:12
bb,size:13
cc,size:3

I want to delete size: , and the file will become like this:

aa,12
bb,13
cc,3

Can anyone tell me how to use shell script to perform this task?

migrated from serverfault.com Sep 28 '15 at 16:49

This question came from our site for system and network administrators.

12

You can use sed easily for this task:

sed -i -- 's/size://' abc.csv

s/size:// is a simple regex that says replace size: with <blank>.

  • 1
    Perfect ! It works on me. Thank You so much. :) – technoob Sep 25 '15 at 13:43
4

Below is a useful alternative command for Unix users whose version of sed does not support the -i flag (such as Solaris -- and also OS X, which ships with BSD sed by default).

sed 's/size://' abc.csv > tmpfile; mv tmpfile abc.csv

The g option at the end of the sed command is not required in your specific example, because 'size:' doesn't appear more than once per line.

EDIT: For smaller files, you could also store the updated content in a variable, in order to avoid creating an unnecessary temporary file:

new="$(sed 's/size://' abc.csv)"; echo "$new" > abc.csv

This method would also allow the ability to echo the "$new" variable (updated file) to STDOUT for sanity checking purposes before overwriting the original file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy