Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sed to Search 2 patterns, if the first pattern not exist, print the second pattern

Tags:

sed

I have a question on sed. From this input:

[/=TueGreen$58.30Orange5:36pmSat*=WedOrange$76.63Purple6:20pmTue]
[/=Thu6:06pm$09.05Blue11:32amMon/=Thu1:38am$56.41Red4:25amThu]
[/=Sun1:49pm$12.41Yellow2:51pmMon*=FriOrange$49.68Blue1:24pmTue]
[/=Sat11:58am$82.24Orange3:44amMon*=Thu1:08am$33.49Red8:21amSat]

I have to make an Output:

$58.30
6:06pm$09.05
1:49pm$12.41
11:58am$82.24

I know, the pattern seems so easy, but, I'm even failed in finding the time pattern. Because the hour is sometimes 1 or 2 digits.

This is the third day I've been learning sed and looking for the answer. I've learned grep. So easy if using grep. But this assignment force me to use sed. So far, this is my sed command:

sed 's/.*\([0-9]*:[0-9]*\(am\|pm\)\).*/\1/' FILE

The results are only showing the minutes and am/pm. There are many time pattern in each line of the input. But my result shows the last time pattern in each line. As you see below:

:20pm
:25am
:24pm
:21am

Where did I go wrong?

like image 412
mprabuw Avatar asked Dec 17 '13 20:12

mprabuw


1 Answers

Here you go:

sed -e 's/[^$0-9]*\([0-9:]*[ap]m\)*\(\$[0-9.]*\).*/\1\2/'
like image 74
janos Avatar answered Oct 22 '22 07:10

janos