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 :
문제
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 ?
해결책
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 :
다른 팁
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