Linux中用制表符替换空格

发布于 2024-08-04 22:25:22 字数 36 浏览 5 评论 0原文

如何在 Linux 中将给定文本文件中的空格替换为制表符?

How do I replace whitespaces with tabs in linux in a given text file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

傲鸠 2024-08-11 22:25:22

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

Use the unexpand(1) program


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'').
叶落知秋 2024-08-11 22:25:22

您可以尝试使用 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

I think you can try with awk

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

or SED if you preffer

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

or even tr

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

or a simplified version of the tr solution sugested by Sam Bisbee

tr ' ' \\t < someFile > someFile
梦亿 2024-08-11 22:25:22

使用 Perl:

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

Using Perl:

perl -p -i -e 's/ /\t/g' file.txt
林空鹿饮溪 2024-08-11 22:25:22

更好的 tr 命令:

tr [:blank:] \\t

这将清理 unzip -l 的输出,以便使用 grep、cut 等进一步处理,

例如,

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

better tr command:

tr [:blank:] \\t

This will clean up the output of say, unzip -l , for further processing with grep, cut, etc.

e.g.,

unzip -l some-jars-and-textfiles.zip | tr [:blank:] \\t | cut -f 5 | grep jar
若有似无的小暗淡 2024-08-11 22:25:22

将当前目录下的每个 .js 文件转换为制表符的示例命令(仅转换前导空格):

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

Example command for converting each .js file under the current dir to tabs (only leading spaces are converted):

find . -name "*.js" -exec bash -c 'unexpand -t 4 --first-only "$0" > /tmp/totabbuff && mv /tmp/totabbuff "$0"' {} \;
爱给你人给你 2024-08-11 22:25:22

下载并运行以下脚本,以递归方式将纯文本文件中的软选项卡转换为硬选项卡。

从包含纯文本文件的文件夹内放置并执行脚本。

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

Download and run the following script to recursively convert soft tabs to hard tabs in plain text files.

Place and execute the script from inside the folder which contains the plain text files.

#!/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;
往事风中埋 2024-08-11 22:25:22

这会将连续的空格替换为一个空格(但不是制表符)。

tr -s '[:blank:]'

这将用制表符替换连续的空格。

tr -s '[:blank:]' '\t'

This will replace consecutive spaces with one space (but not tab).

tr -s '[:blank:]'

This will replace consecutive spaces with a tab.

tr -s '[:blank:]' '\t'
苍景流年 2024-08-11 22:25:22

使用sed

T=$(printf "\t")
sed "s/[[:blank:]]\+/$T/g"

sed "s/[[:space:]]\+/$T/g"

Using sed:

T=$(printf "\t")
sed "s/[[:blank:]]\+/$T/g"

or

sed "s/[[:space:]]\+/$T/g"
擦肩而过的背影 2024-08-11 22:25:22

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

You can also use astyle. I found it quite useful and it has several options too:

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.`
空心↖ 2024-08-11 22:25:22

如果您正在谈论用制表符替换一行上的所有连续空格,则 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  

如果您正在讨论修复选项卡损坏的文件,请使用其他答案中提到的 expandunexpand

If you are talking about replacing all consecutive spaces on a line with a tab then 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

If you are talking about replacing all whitespace (e.g. space, tab, newline, etc.) then 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  

If you are talking about fixing a tab-damaged file then use expand and unexpand as mentioned in other answers.

陈年往事 2024-08-11 22:25:22
sed 's/[[:blank:]]\+/\t/g' original.out > fixed_file.out

例如,这将减少制表符的数量……或将空格减少到一个制表符中。

您还可以针对将多个空格/制表符合并到一个空格的情况执行此操作:

sed 's/[[:blank:]]\+/ /g' original.out > fixed_file.out
sed 's/[[:blank:]]\+/\t/g' original.out > fixed_file.out

This will for example reduce the amount of tabs.. or spaces into one single tab.

You can also do it for situations of multiple spaces/tabs into one space:

sed 's/[[:blank:]]\+/ /g' original.out > fixed_file.out
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文