Home > bash > sed – Substituting text on certain lines

sed – Substituting text on certain lines

Problem:
Needed to remove sales totals for some days in a month. Had to change from

shell> less Oct2011_totals.csv
2011-10-01,586
2011-10-02,399
2011-10-03,761
2011-10-04,517
2011-10-05,773
2011-10-06,1882
2011-10-07,1759
2011-10-08,1948
2011-10-09,111258
2011-10-10,105451
2011-10-11,129304
2011-10-12,131212
2011-10-13,140890
2011-10-14,132396
2011-10-15,0
2011-10-16,0
2011-10-17,0
2011-10-18,0
2011-10-19,0
2011-10-20,0
2011-10-21,0
2011-10-22,0
2011-10-23,0
2011-10-24,0
2011-10-25,0
2011-10-26,0
2011-10-27,0
2011-10-28,0
2011-10-29,0
2011-10-30,0
2011-10-31,0

To

2011-10-01,0
2011-10-02,0
2011-10-03,0
2011-10-04,0
2011-10-05,0
2011-10-06,0
2011-10-07,0
2011-10-08,0
2011-10-09,111258
2011-10-10,105451
2011-10-11,129304
2011-10-12,131212
2011-10-13,140890
2011-10-14,132396
2011-10-15,0
2011-10-16,0
2011-10-17,0
2011-10-18,0
2011-10-19,0
2011-10-20,0
2011-10-21,0
2011-10-22,0
2011-10-23,0
2011-10-24,0
2011-10-25,0
2011-10-26,0
2011-10-27,0
2011-10-28,0
2011-10-29,0
2011-10-30,0
2011-10-31,0

Solution:
sed looked like a simple enough solution.

shell> sed  '1,8 s/\(2011-10-[0-9]\+\),[0-9]\+$/\1,0/' Oct2011_totals.csv > temp_file && mv temp_file Oct2011_totals.csv
Advertisements
Categories: bash Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: