Why is sed not recognizing \t as a tab?

前端 未结 11 1592
伪装坚强ぢ
伪装坚强ぢ 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:40

    sed doesn't support \t, nor other escape sequences like \n for that matter. The only way I've found to do it was to actually insert the tab character in the script using sed.

    That said, you may want to consider using Perl or Python. Here's a short Python script I wrote that I use for all stream regex'ing:

    #!/usr/bin/env python
    import sys
    import re
    
    def main(args):
      if len(args) < 2:
        print >> sys.stderr, 'Usage:  '
        raise SystemExit
    
      p = re.compile(args[0], re.MULTILINE | re.DOTALL)
      s = sys.stdin.read()
      print p.sub(args[1], s),
    
    if __name__ == '__main__':
      main(sys.argv[1:])
    

提交回复
热议问题