6

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?

2 Answers 2

13

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
  • 1
    Perfect ! It works on me. Thank You so much. :)
    – technoob
    Commented Sep 25, 2015 at 13:43
5

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.

You must log in to answer this question.