I have files containing data. This data sometimes has artifacts in them which I need to get rid of. An example line looks like this:


Or like this:


In both cases some unwanted characters are in front of the date string. I need to remove those and keep everything else. Sometimes these artifacts are not in the date column, but in one of the other columns.

What I tried is using sed like this:

sed 's/[^0-9:_.,]*//g' dat.log > test.log

The aim was to delete everything but numbers, colons, underscores, dots and commas. And that works just fine. The problem is that sed doesn't write back the newline. I know that it takes it off during the process, but how can I get it back when the line is written to a file?

edit: Added more lines to the example input and added output from my sed command:


edit2: It turned out the problem was that the Raspberry Pi where the data is coming from, saved the files with the macintosh dataset. No idea why, but changing that using tr '\r' '\n' < macfile.txt > unixfile.txt first solved the problem.

  • sed would write back the newline, a Unix newline. If your original file is a DOS text file, then it would get converted to a Unix text file by your sed command since you would delete the carriage return at the end of each line. Do edit your question and add examples of what you're seeing.
    – Kusalananda
    Aug 9, 2018 at 8:24
  • You must give an actual example, because poor sed works line by line -- it reads line, and it writes lines, complete with terminating newline. It cannot not write a newline after every line it outputs.
    – AlexP
    Aug 9, 2018 at 8:25
  • added more input lines and the output from my command @AlexP. Files are coming from a Raspberry Pi and never saw DOS. Aug 9, 2018 at 8:30
  • Cannot reproduce the behavior. On what operating system are you seeing this apparently impossible behavior?
    – AlexP
    Aug 9, 2018 at 8:36
  • 1
    Post a hexdump of your file.
    – RudiC
    Aug 9, 2018 at 8:55

2 Answers 2


Here it seems like you can do:

tr -cd '0-9:_.,\r\n' < file.in > file.out

To remove all but those characters you want to keep including the line delimiters in whatever format they were initially.

  • That works great! And is a lot faster compared to sed! Aug 9, 2018 at 9:24

Why don't you get rid of the tr and include that in the sed command:

sed 's/\(^\|^M\)[^0-9:_.,]*/\n/g; s/^\n//' file

May be a bit tricky to include the control characters (^M, \r, 0x0D) and (\n, 0x0A) in the sed script, depending on your OS and sed version.

  • Simply because I have no clue how that works. :) Your command doesn't do anything. The characters are still there and it is still in the macintosh char set. Aug 9, 2018 at 9:22
  • You can enter control characters on the shell (bash) command line like Ctrl-V Ctrl-M for ^M and Ctrl-V Ctrl-J for \n.
    – RudiC
    Aug 9, 2018 at 10:56

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.