1

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 ?

1
  • 2
    It would be better if you indicate how you are obtaining such line, because we could try to change the date output format. Commented Jan 14, 2014 at 14:27

4 Answers 4

2

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 :
Sign up to request clarification or add additional context in comments.

1 Comment

+1, this is the most efficient way unlike spawning other processes like sed.
1

Using sed:

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

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.