add control characters using sed with escape sequences?

add control characters using sed with escape sequences?

Post by Pau » Wed, 14 Aug 2002 14:32:10



Ok, I've managed to pull out most of the hair on my head over this
one...

I want to do the opposite to what most people seem to be asking.  I
have an ascii text file on my unix server and I wish to add Ctrl-M
(^M) characters to the end of each line in the file.  There are no ^M
characters in the file presently.

I can do this rather easily using the following sed command,

  sed -e 's/$/^M/' infile > outfile

where ^M is entered as a control character using Ctrl-V + Ctrl-M.

However, what I need to do is create a script that does not contain
the control character ^M in the sed command.  I would like to use an
escape sequence for the ^M character so that my script does not
contain any control characters.

Is that possible using sed?  I have seen many posts that mention it is
hard to use escape sequences in sed.

An option might be to use the translate command "tr" however I have
had no luck using this command.

Help!?

Regards
Paul

 
 
 

add control characters using sed with escape sequences?

Post by mats.blomstr.. » Wed, 14 Aug 2002 18:14:52



>   sed -e 's/$/^M/' infile > outfile
> However, what I need to do is create a script that does not contain
> the control character ^M in the sed command.  I would like to use an
> escape sequence for the ^M character so that my script does not
> contain any control characters.
> Is that possible using sed?

I dont think so. You can use a scriptfile if you want avoid
control-characters on the command line (ie, sed -f <script-file> )

I think that "unix2dos" is doing what you want.

        bash$ echo "A string" > testfile

        bash$ od -c testfile
        0000000   A       s   t   r   i   n   g  \n
        0000011

        bash$ unix2dos testfile
        unix2dos: converting file testfile to DOS format ...

        bash$ od -c testfile
        0000000   A       s   t   r   i   n   g  \r  \n
        0000012

        bash$ echo -n "^M" >>testfile

        bash$ od -c testfile
        0000000   A       s   t   r   i   n   g  \r  \n  \r
        0000013

//Mats

 
 
 

1. Using sed with control/escape characters

The following statement works fine when entered from a Unix command
line:

sed 's/^L//' oldfile >newfile  (where the ^L was composed with a Ctrl V
and Ctrl L key sequence)

However, when I copy this into a script (#!/bin/sh) it does not work
(i.e. it does not remove all the ^L in the file)

I have also tried sed 's/\cL//' and sed 's/\014//' but they don't work
from the command line or in the script.

Thanks for helping.

2. Could you tell me what that is?

3. mapping escape sequences to control characters in KSH

4. I cannot rlogin or telnet into RS6000 anymore

5. Escape sequences for color selection in character mode.

6. filesystem damaged, maybe by Win32s?

7. Insert escape sequence in file : SED ?

8. Stack size limit

9. insert escape sequence in a file : SED ?

10. Escape sequence to flush characters

11. Can gnome terminal expand to 132 characters with xterm escape sequence ?

12. escape sequence characters

13. multi-line patterns in gnu-sed / gnu-bash - how to escape that `0 sequence?