I'm looking for an awk or sed solution, I'm not a genius with either. I have read all of the suggestions that came up when I started typing this up.

I have a file with lines of data:

 thisdate=Sep  2 23:59:59 2022 GMT
 thisdate=Sep 15 23:59:59 2022 GMT

The names and dates/times are all different, the day numbers are always in the same place and always take up the same number of characters (2), the hours/min/secs are all always in the same position on the line and its not every line, thisdate is always a date in the future, and there are no blank lines.

I am trying to remove the time from those date lines by trying to key off  thisdate= (that's a space in front of thisdate) and then deleting characters in positions 18-26 (if I counted right)

I've tried a couple things but have been mostly searching for something that fits my needs to point me in the right direction

  952  sed 's/^\(.\{18\}\)\(.\{26\}\)/\1/' file  <--this may not have worked as I didn't key on a match?
  958  sed -i 's/thisdate=(.{11})18/\26/' file
  959  sed -i 's/thisdate=(.{10})17/\25/' file

I'm sure I'm missing something but if someone could point me in the right direction I'd be grateful!

  • Do you want to keep the GMT part?
    – Freddy
    Apr 13 '20 at 21:21

It looks like it should be enough with just deleting 9 characters after the 6th character after  thisdate=:

sed 's/^\( thisdate=.\{6\}\).\{9\}/\1/' file

For the given data, this would generate

 thisdate=Sep  2 2022 GMT
 thisdate=Sep 15 2022 GMT
  • Thank you! That worked perfectly for what I needed!
    – Staci
    Apr 13 '20 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.