0

I've got a txt file that I've retrieved from SQL Server Manager and hence the line delimitor is '\r\n'. I would like to change this line delimitor to '\n'. You can see my approach below. The problem is that even afer running the sed, it looks like '\r\n' was not replaced, as you can see below. What am I missing out here?

>>> grep \r\n some_file.txt |wc -l
21321

>>>sed -i 's/\r\n/\n/g' some_file.txt
>>> grep \r\n some_file.txt |wc -l
21321

I am using:

sed (GNU sed) 4.4

on Linux 4.15 Ubuntu

callmeGuy
  • 944
  • 2
  • 11
  • 28
  • 1
    Just use `sed -i 's/\r//g' some_file.txt` as sed operates on a per line basis. – Wiktor Stribiżew May 14 '19 at 14:43
  • 1
    To give a little insight: `sed` removes the standard `\n` line termination from your file input during script execution and re-appends it on output. In between, it only holds `…\r` in its pattern space, which is why you can never match `\r\n` at the end of a line – there is no `\n`. You need to match for a single `\r` at the end of line and replace that with an empty string. – Robin479 May 14 '19 at 15:25
  • 1
    to extend Robin479's comment, you can match the end of the line with the `$` char, so `\r$` . Good luck. – shellter May 14 '19 at 16:42
  • @shellter still not working – callmeGuy May 15 '19 at 11:56
  • @Robin479 it did that too, but still dos not work – callmeGuy May 15 '19 at 11:57
  • 1
    Come to think of it, I don't think `sed` honnors `\r`, `\n` sort of "notation", you'll need to specify the Ctrl Char version, and because of various historical contstraints, `\r=^M`, while `\n=^J`. If you're using a `vi` based cmd-line editor, you need to escape those chars with a `^V`, so, I think you really want `sed 's/^V^M$//' file` (note the `g` is not needed). And that means hold down Ctrl key, press `v`, keep Ctrl key down, press `m`. You won't see the `^V` appear on the cmd-line. ..... – shellter May 15 '19 at 13:50
  • 1
    And, of course, assuming that you are only looking to convert a `DOS` file to `UNIX` format, your system may have the utility built just for this purpose, `dos2unx file [files....n]` . OR if you're on a primitive system, hopefully you can do `tr -d '\013' file > file.tmp && mv file.tmp file`. If none of this helps, please provide the output of `uname -srv` and `sed --version`. In fact as a newbie, it would be a good idea to make that the first information in all questions you post. Good luck! – shellter May 15 '19 at 13:54
  • @shellter thanks. it turns out I do not have dos2unx. In the same time sed 's/^V^M$//' did not do it and the tr gives me a syntax error – callmeGuy May 15 '19 at 14:24
  • Sorry, that was a typo that I missed, `dos2unix file [file(s).....]` . OR please paste exact error message from `tr`. Good luck. – shellter May 15 '19 at 15:17
  • @shellter, got it, however I don't have it.I did howerver mention my linux and sed version – callmeGuy May 16 '19 at 06:49

1 Answers1

1

try this:

sed -i 's/\r//' some_file.txt
UtLox
  • 3,744
  • 2
  • 10
  • 13