How to change what sed thinks is a line separator

Since I'm new to sed, I am very pleased to see that sed does not consider the \ r character to be a valid line delimiter.

Does anyone know how to tell sed which character (s) I would like to use as a line separator when processing many lines of text?

+4
source share
3 answers

You can specify it with the awk RS variable (record separator): awk 'BEGIN {RS = "\r"} ...

Or you can convert with: tr '\r' '\n'

+2
source

(To make the examples below clearer and less ambiguous, I will use od util .)

This cannot be done with a flag, for example. I bet the best solution is the one quoted from the previous answers: using tr . If you have a file, for example:

 $ od -xc slashr.txt 0000000 6261 0d63 6564 0d66 abc \rdef \r 0000010 

There are various ways to use tr ; the one we wanted is to pass two parameters to it - two different characters - and tr will replace the first parameter with the second. By sending the contents of the file as input to tr '\r' '\n' , we get the following result:

 $ tr '\r' '\n' < slashr.txt | od -xc 0000000 6261 0a63 6564 0a66 abc \ndef \n 0000010 

Excellent! Now we can use sed :

 $ tr '\r' '\n' < slashr.txt | sed 's/^./#/' #bc #ef $ tr '\r' '\n' < slashr.txt | sed 's/^./#/' | od -xc 0000000 6223 0a63 6523 0a66 # bc \n # ef \n 0000010 

But I suppose you need to use \r as a line separator, right? In this case, just use tr '\n' '\r' to reverse the conversion:

 $ tr '\r' '\n' < slashr.txt | sed 's/^./#/' | tr '\n' '\r' | od -xc 0000000 6223 0d63 6523 0d66 # bc \r # ef \r 0000010 
+1
source

As far as I know, you cannot. What is wrong with using a new line as a separator? If your entry has a line ending in DOS format, you can pre-process it to remove them, and, if necessary, they can be returned later.

0
source

All Articles