Why is sed not recognizing \t as a tab?

前端 未结 11 1545
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 19:54
sed \"s/\\(.*\\)/\\t\\1/\" $filename > $sedTmpFile && mv $sedTmpFile $filename

I am expecting this sed script to insert a <

相关标签:
11条回答
  • 2020-11-29 20:42

    I used this on Mac:

    sed -i '' $'$i\\\n\\\thello\n' filename
    

    Used this link for reference

    0 讨论(0)
  • 2020-11-29 20:46

    You don't need to use sed to do a substitution when in actual fact, you just want to insert a tab in front of the line. Substitution for this case is an expensive operation as compared to just printing it out, especially when you are working with big files. Its easier to read too as its not regex.

    eg using awk

    awk '{print "\t"$0}' $filename > temp && mv temp $filename
    
    0 讨论(0)
  • 2020-11-29 20:46
    TAB=$(printf '\t')
    sed "s/${TAB}//g" input_file
    

    It works for me on Red Hat, which will remove tabs from the input file.

    0 讨论(0)
  • 2020-11-29 20:47

    Use $(echo '\t'). You'll need quotes around the pattern.

    Eg. To remove a tab:

    sed "s/$(echo '\t')//"
    
    0 讨论(0)
  • 2020-11-29 20:50

    Not all versions of sed understand \t. Just insert a literal tab instead (press Ctrl-V then Tab).

    0 讨论(0)
提交回复
热议问题