[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Inserting a <tab> into sed's replacement string



bob parker wrote:
> I want to be able to insert tab chars into the output lines eg
> sed = somefile | sed 'N;s/\n/\t/'
> just gives me the literal 't' immediately following the number at the start 
> of each line.
> [...]
> Is there some way of inserting the tab in octal or hex?

This comes up periodically.  Here are the two prefered methods.

  1. Works way back to UNIX V7 systems (1978).  So if you want maximal
     portability even to strange systems this one is the want you
     want.

    TAB=`awk 'BEGIN{printf "\t";}'`

  2. This next is my personal preference.  The 'printf' command is
     XPG4 (1992) but many older hosts don't have it.  But you can
     always add it to even old systems.  Therefore it seems more
     modern and more standard.

    TAB=$(printf "\t")

  Then once you have TAB available, just use it.  Your example would
  become:

    TAB=$(printf "\t")
    sed = somefile | sed "N;s/\n/$TAB/"

The need in the bash shell to use -e to expand escape sequences is in
conflict with the current POSIX standards.  Therefore I avoid it since
it is not portable even though the standards say it should be.

Bob

Attachment: pgppkJkYJewbt.pgp
Description: PGP signature


Reply to: