V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
alphonsez
V2EX  ›  问与答

有没有什么命令找出混用了 tab 和空格作为缩紧的文件?

  •  
  •   alphonsez · Aug 17, 2015 · 3484 views
    This topic created in 3910 days ago, the information mentioned may be changed or developed.

    python 被改坏了。。。

    8 replies    2015-08-20 00:53:14 +08:00
    Theo14
        1
    Theo14  
       Aug 17, 2015
    用 editplus 的正则表达式查找所有 tab 再替换成(四个?)空格
    loading
        2
    loading  
       Aug 17, 2015 via Android
    楼上正解!
    @Theo14
    alphonsez
        3
    alphonsez  
    OP
       Aug 17, 2015
    说是不能全改已有的文件,要和文件本来的保持一致。另外,在服务器上……

    于是我这么干了(大改如此):

    grep -ril '\t' * | sort > a.txt
    grep -ril ' ' * | sort > b.txt
    comm -1 -2 a.txt b.txt

    然后把这些改坏的文件给手动修复了(用了 sed )。
    kodango
        4
    kodango  
       Aug 17, 2015
    cat -A yourfile.py
    或者
    vim yourfile.py ,让后 :set list
    yakczh
        5
    yakczh  
       Aug 17, 2015
    notepad++ 菜单栏选设置->首选项->制表符设置->制表符宽度输入 4 选中[转换为空格 ]
    alphonsez
        6
    alphonsez  
    OP
       Aug 18, 2015
    @kodango 问题是,有一大堆文件要检查。
    kodango
        7
    kodango  
       Aug 19, 2015   ❤️ 1
    @alphonsez

    $ find . -name '*.py' | xargs -I{} awk 'BEGIN { has_tab = 0; has_space = 0; }
    {
    if ($0 ~ /^\t+ / || $0 ~ /^ +\t/) { /* mixed tab and space in one line */
    printf "%s: found mixed tab and space\n", FILENAME;
    nextfile;
    } else if ($0 ~ /^\t/) { /* find tab in one line */
    if (has_space == 1 ) {
    printf "%s: found mixed tab and space\n", FILENAME;
    nextfile;
    } else {
    has_tab = 1;

    }
    } else if ($0 ~ /^ /) { /* find space in one line */
    if (has_tab == 1 ) {
    printf "%s: found mixed tab and space\n", FILENAME;
    nextfile;
    } else {
    has_space = 1;
    }
    }
    }' {}

    简单测试过,命令的用法。。感觉比较恶心,不知道有没有方便的方法。。
    alphonsez
        8
    alphonsez  
    OP
       Aug 20, 2015
    @kodango 赞,多谢, awk 好强大
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2417 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 03:52 · PVG 11:52 · LAX 20:52 · JFK 23:52
    ♥ Do have faith in what you're doing.