7

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?

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
zav
  • 71
  • 1
  • 2
  • You could always do multiple passes, and swap your characters around, then back again - See http://stackoverflow.com/questions/1251999/ for the first part of this – Merlyn Morgan-Graham Aug 02 '11 at 23:38
  • how are you using sed? from a windows cmd line, with a sed.exe port to Windows? or thru Cygwin? or ?? If you are using ftp to move files from Windows to Unix and then seding the file, there is an option on ftp that will convert between systems. Good luck. – shellter Aug 03 '11 at 00:27
  • Maybe, it's just `\r` not being a valid line delimiter? Unless you're using original Mac OS, of course. – Catherine Aug 03 '11 at 14:24
  • Does this answer your question? [Using different delimiters in sed commands and range addresses](https://stackoverflow.com/questions/5864146/using-different-delimiters-in-sed-commands-and-range-addresses) – tripleee Feb 17 '21 at 07:02
  • @tripleee No, that cannot be. sed operates on lines by default, so what the OP is asking is how to *define lines differently* so that `sed` will interpret the beginning and end of a line differently than would be the case when using \n or \r\n. For example, if you wanted to strip out \n from a string piped to sed. – Anthony Rutledge Jul 26 '21 at 13:12
  • @Anthony I'm not sure how you can infer that from the question; it's quite unclear actually. – tripleee Jul 26 '21 at 15:46

3 Answers3

2

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

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

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
2

(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
brandizzi
  • 26,083
  • 8
  • 103
  • 158
-2

As far as I know, you can't. What's wrong with using a newline as the delimiter? If your input has DOS-style \r\n line endings it can be preprocessed to remove them and, if necessary, they can be returned afterwards.

sorpigal
  • 25,504
  • 8
  • 57
  • 75
  • sometimes people might want null delimiters in situations where newline is a valid character in the input (like a list of filenames in many \*nix filesystems) – jw013 Aug 03 '11 at 17:02
  • You might be trying to strip out newline characters, in which case `sed` would be blind. – Anthony Rutledge Jul 26 '21 at 13:08