【发布时间】:2009-09-14 21:57:13
【问题描述】:
如何在给定的文本文件中用 linux 中的制表符替换空格?
【问题讨论】:
标签: linux tabs whitespace
如何在给定的文本文件中用 linux 中的制表符替换空格?
【问题讨论】:
标签: linux tabs whitespace
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'').
【讨论】:
tr 或sed。
我觉得你可以试试 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),那么该行将从结果中省略。
使用 Perl:
perl -p -i -e 's/ /\t/g' file.txt
【讨论】:
perl -p -i -e 's/\t/ /g' *.java
s/ {4}/ 将 4 空格缩进转换为制表符。
更好的tr命令:
tr [:blank:] \\t
这将清理 unzip -l 的输出,以便使用 grep、cut 等进行进一步处理。
例如,
unzip -l some-jars-and-textfiles.zip | tr [:blank:] \\t | cut -f 5 | grep jar
【讨论】:
tr [:blank:] \\t
将当前目录下的每个.js文件转换为制表符的示例命令(仅转换前导空格):
find . -name "*.js" -exec bash -c 'unexpand -t 4 --first-only "$0" > /tmp/totabbuff && mv /tmp/totabbuff "$0"' {} \;
【讨论】:
下载并运行以下脚本,以递归方式将纯文本文件中的软标签转换为硬标签。
在包含纯文本文件的文件夹中放置并执行脚本。
#!/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.`
【讨论】:
使用 sed:
T=$(printf "\t")
sed "s/[[:blank:]]\+/$T/g"
或
sed "s/[[:space:]]\+/$T/g"
【讨论】:
如果您说的是用制表符替换一行中的所有连续空格,那么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 会替换为 非空格的连续字符。
sed 's/[[:blank:]]\+/\t/g' original.out > fixed_file.out
这将例如减少制表符的数量..或空格到一个制表符中。
你也可以针对多个空格/制表符合并为一个空格的情况:
sed 's/[[:blank:]]\+/ /g' original.out > fixed_file.out
【讨论】: