What does this SED command do and how can I modify it for my use case?


I have been asked to fix someone else code so im unsure how the command actually works as ive never had to work with regex type code.

sed -r 's/([0-9]{2})\/([0-9]{2})\/([0-9]{4})\s([0-9]{2}:[0-9]{2}:[0-9]{2})/\3\/\1\/\2 \4/g'

This code reads the below txt file and is ‘meant’ to display the number in bold below.

placeholder_name                    01/01/2022 12:00:00  01/01/2022 12:00:01  STATUS    12345/15     50

This is output to a new temp file but the issue is that only the first character in the number after the ‘/’ is displayed, i.e. for the above example only 1 is displayed.

How would I modify the above command to take the full number after the ‘/’. Alternatively, if there is a nicer/better way to do this id be happy to hear it

Note: The number in bold has a range of 1-99


Using sed

$ sed -E 's#.*/([[:digit:]]+).*#\1#' input_file

Answered By – HatLess

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published