Search code examples
bashshellcut

Cutting part of line/string in shell scripting


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 ?


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 :