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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI 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?
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>
.
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.