Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change what sed thinks is the line delimiter

Tags:

sed

As I'm new with sed, I'm having the fun of seeing that sed doesn't think that the \r character is a valid line delimiter.

Does anyone know how to tell sed which character(s) I'd like it to use as the line delimiter when processing many lines of text?

like image 995
zav Avatar asked Aug 02 '11 23:08

zav


People also ask

How do you specify a delimiter in sed?

sed Substitution Using different delimitersAny character other than backslash or newline can be used instead of a slash to delimit the BRE and the replacement. Within the BRE and the replacement, the BRE delimiter itself can be used as a literal character if it is preceded by a backslash.

Does sed work line-by-line?

The sed command, short for stream editor, performs editing operations on text coming from standard input or a file. sed edits line-by-line and in a non-interactive way.

How do you put a character at the end of a line in sed?

Using a text editor, check for ^M (control-M, or carriage return) at the end of each line. You will need to remove them first, then append the additional text at the end of the line. Save this answer.

What is I flag in sed?

The I flag allows to match a pattern case insensitively. Usually i is used for such purposes, grep -i for example. But i is a command (discussed in append, change, insert chapter) in sed , so /REGEXP/i cannot be used. The substitute command does allow both i and I to be used, but I is recommended for consistency.


2 Answers

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

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

like image 161
glenn jackman Avatar answered Oct 05 '22 09:10

glenn jackman


(For making the examples below clearer and less ambiguous, I'll use the od util extensively.)

It is not possible to do with a flag, for example. I bet the best solution is the one cited by the previous answers: using tr. If you have a file such as the one below:

$ od -xc slashr.txt
0000000      6261    0d63    6564    0d66                                
           a   b   c  \r   d   e   f  \r                                
0000010

There are various ways of using tr; the one we wanted is to pass two parameters for it - two different chars - and tr will replace the first parameter by the second one. Sending the file content as input for tr '\r' '\n', we got the following result:

$ tr '\r' '\n' < slashr.txt | od -xc 
0000000      6261    0a63    6564    0a66                                
           a   b   c  \n   d   e   f  \n                                
0000010

Great! 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                                
           #   b   c  \n   #   e   f  \n                                
0000010

But I presume you need to use \r as the line delimiter, 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                                
           #   b   c  \r   #   e   f  \r                                
0000010
like image 25
brandizzi Avatar answered Oct 05 '22 10:10

brandizzi