How can I insert a tab character with sed on OS X?

后端 未结 6 1498
野的像风
野的像风 2020-11-29 23:20

I have tried:

echo -e \"egg\\t  \\t\\t salad\" | sed -E \'s/[[:blank:]]+/\\t/g\'

Which results in:

eggtsalad
6条回答
  •  野性不改
    2020-11-30 00:04

    A workaround for tab on osx is to use "\ ", an escape char followed by four spaces.

    If you are trying to find the last instance of a pattern, say a " })};" and insert a file on a newline after that pattern, your sed command on osx would look like this:

    sed -i '' -e $'/^\    \})};.*$/ r fileWithTextIWantToInsert' FileIWantToChange
    

    The markup makes it unclear: the escape char must be followed by four spaces in order for sed to register a tab character on osx.

    The same trick works if the pattern you want to find is preceded by two spaces, and I imagine it will work for finding a pattern preceded by any number of spaces as well.

提交回复
热议问题