Question

I have the following line:

Jan 13, 2014 1:01:31 AM

I want to remove the seconds part of the line. The result should be:

Jan 13, 2014 1:01 AM

How can this be done ?

Was it helpful?

Solution

Use parameter expansion:

t='Jan 13, 2014 1:01:31 AM'
ampm=${t: -2}               # last two characters
echo "${t%:*} $ampm"        # remove everything after the last :

OTHER TIPS

Using sed:

s='Jan 13, 2014 1:01:31 AM'
sed 's/:[0-9]*\( [AP]M\)/\1/' <<< "$s"
Jan 13, 2014 1:01 AM

you can give this a try:

sed 's/:[^:]* / /'

with your example:

kent$ (master|✚2) echo "Jan 13, 2014 1:01:31 AM"|sed 's/:[^:]* / /'
Jan 13, 2014 1:01 AM

Another way, if your date command is gnu date which support -d option.

$ str="Jan 13, 2014 1:01:31 AM"

$ date -d "$str" +"%b %d, %Y %l:%M %p"

Jan 13, 2014  1:01 AM
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top