2011-08-19 103 views
0

有沒有辦法讓這個正則表達式在開始處放置$ {color orange},並在找到該日期的行的末尾放置$ color?

DJS=`date +%_d`; 
cat thisweek.txt | sed s/"\(^\|[^0-9]\)$DJS"'\b'/'\1${color orange}'"$DJS"'$color'/ 

有了這個表達我得到這個:

Saturday Aug 13 12pm - 9pm 4pm - 5pm
Sunday Aug 14 9:30am - 6pm 1pm - 2pm
Monday Aug 15 6:30pm - 11:30pm None
Tuesday Aug 16 6pm - 11pm None
Wednesday Aug 17 Not Currently Scheduled for This Day
Thursday Aug ${color orange}18$color Not Currently Scheduled for This Day
Friday Aug 19 7am - 3:30pm 10:30am - 11:30am

我想擁有的是:

Saturday Aug 13 12pm - 9pm 4pm - 5pm Sunday Aug 14 9:30am - 6pm 1pm - 2pm
Monday Aug 15 6:30pm - 11:30pm None
Tuesday Aug 16 6pm - 11pm None
Wednesday Aug 17 Not Currently Scheduled for This Day
${color orange}Thursday Aug 18 Not Currently Scheduled for This Day$color
Friday Aug 19 7am - 3:30pm 10:30am - 11:30am

+0

你基本上有釘;如果這對你不起作用,我懷疑這是因爲不兼容的正則表達式構造 - 我不認爲我已經看到了例如'grod \ b'的'sed'。此外,失去無用的貓。 – tripleee

+0

重新編輯:啊。那麼,從原來的措辭來看,這不太容易知道;)我會更新我的答案 – carlpett

回答

1

Acually,它爲我工作。根據您的 sed版本,您可能需要通過 -r。此外,作爲tripleee說,不使用 cat這裏

DJS=`date +%_d` 
sed -r s/"\(^\|[^0-9]\)$DJS"'\b'/'\1${color orange}'"$DJS"'$color'/ thisweek.txt 

編輯:好了,用新的信息,我來到這個:

sed -r "s/([^0-9]+19.+)/\${color orange}\1\$color/" thisweek.txt 

這給了我輸出

Saturday Aug 13 12pm - 9pm 4pm - 5pm 
Sunday Aug 14 9:30am - 6pm 1pm - 2pm 
Monday Aug 15 6:30pm - 11:30pm None 
Tuesday Aug 16 6pm - 11pm None 
Wednesday Aug 17 Not Currently Scheduled for This Day 
Thursday Aug 18 Not Currently Scheduled for This Day 
${color orange}Friday Aug 19 7am - 3:30pm 10:30am - 11:30am $color 

(請注意,由於它至少在我的時區是星期五,因此與您的區別不同)

+0

真棒謝謝,正是我所需要的。 – TooTallGavin