在Linux中选项卡代替空格(Replace whitespaces with tabs in li

2019-07-18 01:30发布

我如何替换在linux标签空格在一个给定的文本文件?

Answer 1:

使用unexpand(1)程序


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'').


Answer 2:

我想你可以使用awk尝试

awk -v OFS="\t" '$1=$1' file1

或SED如果你preffer

sed 's/[:blank:]+/,/g' thefile.txt > the_modified_copy.txt

甚至TR

tr -s '\t' < thefile.txt | tr '\t' ' ' > the_modified_copy.txt

或TR溶液的简化版本由Sam比斯比sugested

tr ' ' \\t < someFile > someFile


Answer 3:

更好tr命令:

tr [:blank:] \\t

这将清理发言权的输出, 解压缩-l,以便进一步处理使用grep,剪切等

例如,

unzip -l some-jars-and-textfiles.zip | tr [:blank:] \\t | cut -f 5 | grep jar


Answer 4:

使用Perl:

perl -p -i -e 's/ /\t/g' file.txt


Answer 5:

下载并运行下面的脚本软标签递归转化为硬标签的纯文本文件。

地方,从包含纯文本文件的文件夹内执行脚本。

#!/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;


Answer 6:

为当前目录下将每个js文件为制表符(唯一前导空格被转换)实施例的命令:

find . -name "*.js" -exec bash -c 'unexpand -t 4 --first-only "$0" > /tmp/totabbuff && mv /tmp/totabbuff "$0"' {} \;


Answer 7:

您还可以使用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.`


Answer 8:

这将取代连续的空格用一个空格(但不是标签)。

    tr -cs '[:space:]'


文章来源: Replace whitespaces with tabs in linux