How can I convert tabs to spaces in every file of a directory?

后端 未结 19 1285
既然无缘
既然无缘 2020-12-02 03:48

How can I convert tabs to spaces in every file of a directory (possibly recursively)?

Also, is there a way of setting the number of spaces per tab?

19条回答
  •  眼角桃花
    2020-12-02 04:18

    The use of expand as suggested in other answers seems the most logical approach for this task alone.

    That said, it can also be done with Bash and Awk in case you may want to do some other modifications along with it.

    If using Bash 4.0 or greater, the shopt builtin globstar can be used to search recursively with **.

    With GNU Awk version 4.1 or greater, sed like "inplace" file modifications can be made:

    shopt -s globstar
    gawk -i inplace '{gsub("\t","    ")}1' **/*.ext
    

    In case you want to set the number of spaces per tab:

    gawk -i inplace -v n=4 'BEGIN{for(i=1;i<=n;i++) c=c" "}{gsub("\t",c)}1' **/*.ext
    

提交回复
热议问题