Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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 ?
date
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 :
Add a comment
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
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
dateoutput format.