Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

cut command top remove seconds from time. shell scripting

Lets say we got date and time to a log in the format of

2022-05-18-11:57:140100

I need to remove seconds from this time. It means the final output should be like

 2022-05-18-11:57

I tried the following

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

echo "2022-05-18-11:57:140100" | rev | cut -d/ -f6- | rev`:"

this was not successful and I have no idea how this cut command works. can anybody explain. Thanks in advance

>Solution :

1st solution: With GNU awk you can simply do it like following. Simple explanation would be, set FS and OFS as : and then in main block of awk program decrease NF with 1 and print the line.

echo "2022-05-18-11:57:140100" | awk 'BEGIN{FS=OFS=":"} NF--'

2nd solution: In any awk using awk‘s match function please try following.

echo "2022-05-18-11:57:140100" | awk 'match($0,/.*:/){print substr($0,RSTART,RLENGTH-1)}'

3rd solution: Using sed‘s capability of capturing groups(keep values in temp buffer) please try following code.

echo "2022-05-18-11:57:140100" | sed -E 's/(^[^:]*):([^:]*):.*/\1:\2/'

4th solution(OP’s efforts FIX): Fixing OP’s efforts using rev + cut + rev combination here.

echo "2022-05-18-11:57:140100" | rev | cut -d':' -f2- | rev
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading