Answers:
UNEXPAND(1)                      User Commands                     UNEXPAND(1)
NAME
       unexpand - convert spaces to tabs
SYNOPSIS
       unexpand [OPTION]... [FILE]...
DESCRIPTION
       Convert  blanks in each FILE to tabs, writing to standard output.  With
       no FILE, or when FILE is -, read standard input.
       Mandatory arguments to long options are  mandatory  for  short  options
       too.
       -a, --all
              convert all blanks, instead of just initial blanks
       --first-only
              convert only leading sequences of blanks (overrides -a)
       -t, --tabs=N
              have tabs N characters apart instead of 8 (enables -a)
       -t, --tabs=LIST
              use comma separated LIST of tab positions (enables -a)
       --help display this help and exit
       --version
              output version information and exit
. . .
STANDARDS
       The expand and unexpand utilities conform to IEEE Std 1003.1-2001
       (``POSIX.1'').
              我认为您可以尝试使用awk
awk -v OFS="\t" '$1=$1' file1
或SED(如果您愿意)
sed 's/[:blank:]+/,/g' thefile.txt > the_modified_copy.txt
甚至tr
tr -s '\t' < thefile.txt | tr '\t' ' ' > the_modified_copy.txt
或Sam Bisbee提出的tr解决方案的简化版本
tr ' ' \\t < someFile > someFile
              tr ' ' \\t < someFile > someFile
                    ls -l | sed "s/ \+/ /g"
                    awk -v OFS="\t" '$1=$1' file1我注意到,如果你有一个行开始用数字0(例如0   1   2),则该行会从结果ommitted。
                    下载并运行以下脚本,以将软标签递归转换为纯文本文件中的硬标签。
从包含纯文本文件的文件夹内部放置并执行脚本。
#!/bin/bash
find . -type f -and -not -path './.git/*' -exec grep -Iq . {} \; -and -print | while read -r file; do {
    echo "Converting... "$file"";
    data=$(unexpand --first-only -t 4 "$file");
    rm "$file";
    echo "$data" > "$file";
}; done;
              您也可以使用astyle。我发现它非常有用,它也有几个选择:
Tab and Bracket Options:
   If  no  indentation  option is set, the default option of 4 spaces will be used. Equivalent to -s4 --indent=spaces=4.  If no brackets option is set, the
   brackets will not be changed.
   --indent=spaces, --indent=spaces=#, -s, -s#
          Indent using # spaces per indent. Between 1 to 20.  Not specifying # will result in a default of 4 spaces per indent.
   --indent=tab, --indent=tab=#, -t, -t#
          Indent using tab characters, assuming that each tab is # spaces long.  Between 1 and 20. Not specifying # will result in a default assumption  of
          4 spaces per tab.`
              如果您要谈论用制表符替换一行中的所有连续空格,请使用tr -s '[:blank:]' '\t'。
[root@sysresccd /run/archiso/img_dev]# sfdisk -l -q -o Device,Start /dev/sda
Device         Start
/dev/sda1       2048
/dev/sda2     411648
/dev/sda3    2508800
/dev/sda4   10639360
/dev/sda5   75307008
/dev/sda6   96278528
/dev/sda7  115809778
[root@sysresccd /run/archiso/img_dev]# sfdisk -l -q -o Device,Start /dev/sda | tr -s '[:blank:]' '\t'
Device  Start
/dev/sda1       2048
/dev/sda2       411648
/dev/sda3       2508800
/dev/sda4       10639360
/dev/sda5       75307008
/dev/sda6       96278528
/dev/sda7       115809778
如果您要谈论替换所有空格(例如,空格,制表符,换行符等),则tr -s '[:space:]'。
[root@sysresccd /run/archiso/img_dev]# sfdisk -l -q -o Device,Start /dev/sda | tr -s '[:space:]' '\t'
Device  Start   /dev/sda1       2048    /dev/sda2       411648  /dev/sda3       2508800 /dev/sda4       10639360        /dev/sda5       75307008        /dev/sda6     96278528        /dev/sda7       115809778  
如果您正在讨论修复受制表符损坏的文件,请使用expand和unexpand其他答案中所述。
这将用一个空格(而不是制表符)替换连续的空格。
tr -s '[:blank:]'
这将用制表符替换连续的空格。
tr -s '[:blank:]' '\t'
              -c它使用替换不是空格的连续字符。
                    
tror 混为一谈sed。