1

I have a file with the following sample content:

NOF DOWN BITS = 96 data = Wed Jan 3 04:37:32 2018:7E Wed Jan 3 04:37:32 2018:7E Wed Jan 3 04:37:32 2018:7E Wed Jan 3 04:37:32 2018:7E Wed Jan 3 04:37:32 2018:7E Wed Jan 3 04:37:32 2018:7E Wed Jan 3 04:37:32 2018:7E Wed Jan 3 04:37:32 2018:7E Wed Jan 3 04:37:32 2018:7E Wed Jan 3 04:37:32 2018:7E Wed Jan 3 04:37:32 2018:7E Wed Jan 3 04:37:32 2018:7E Wed Jan 3 04:37:32 2018:
nof_received_data_packets

I would like to delete the date and time from the file. That is, I want to remove all instances of "Wed Jan 3 XX:XX:XX 2018:". So, possibly remove n characters after each occurrences of Wed should do.

How would I accomplish it with sed ?

2
  • 1
    is that the actual file? or are there line breaks in it that you've accidentally deleted?
    – cas
    Jan 3, 2018 at 11:03
  • @cas no, there are no line breaks. Thats how the actual logging happens.
    – Prabhu
    Jan 3, 2018 at 14:24

2 Answers 2

2

Just match the pattern:

$ sed 's/Wed Jan 3 ..:..:.. 2018://g' FILE
NOF DOWN BITS = 96 data = 7E 7E 7E 7E 7E 7E 7E 7E 7E 7E 7E 7E 
nof_received_data_packets

There are probably more sophisticated ways to write this, but it does the trick.

0
1

With awk:

awk -v RS='[[:alpha:]]{3} [[:alpha:]]{3} [0-9]{1,2} ([0-9]{2}:?){3} [0-9]{4}:' \
    -v ORS='' '{print}'  datafile

NOF DOWN BITS = 96 data = 7E 7E 7E 7E 7E 7E 7E 7E 7E 7E 7E 7E 
nof_received_data_packets

This works by setting the Record Separator (RS) to a regex that matches strings which look like a date & time followed by a :, and setting the Output Record Separator (ORS) to empty.

Then it just prints each "record".

It works with any date & time, assuming only that short Month names and short Day names are always three letters long, and that the date format is always going to be Day Month Daynum HH:MM:SS YYYY.

With sed:

sed -E 's/[[:alpha:]]{3} [[:alpha:]]{3} [0-9]{1,2} ([0-9]{2}:?){3} [0-9]{4}://g' \
  datafile

This use the same date-matching regex to remove everything that looks like a date & time followed by a :.

With perl:

perl -p -e 's/\w{3} \w{3} \d{1,2} (\d{2}:?){3} \d{4}://g' datafile

perl regular expressions have some nice shortcuts for specifying "word" characters (\w), and digits (\d). The perl version is unicode-aware and should work in any locale.

All three are fairly brute-force scripts. I don't think it's worth the effort of trying anything fancier than that unless the date format was likely to vary from the above. If that was the case, I'd probably write something in perl to scan substrings of each line using the Date::Parse module.

The sed and awk versions require GNU sed and GNU awk, or at least versions of them that understand {n,m} regular expression repetition counts.

1
  • thanks for the solution with awk and sed and for the detailed explanations.
    – Prabhu
    Jan 4, 2018 at 4:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .