在linux中用制表符替换空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1424126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace whitespaces with tabs in linux
提问by biznez
How do I replace whitespaces with tabs in linux in a given text file?
如何在给定的文本文件中用 linux 中的制表符替换空格?
回答by Jonathan
I think you can try with awk
我想你可以试试 awk
awk -v OFS="\t" '=' file1
or SED if you preffer
或 SED 如果您愿意
sed 's/[:blank:]+/,/g' thefile.txt > the_modified_copy.txt
or even tr
甚至 tr
tr -s '\t' < thefile.txt | tr '\t' ' ' > the_modified_copy.txt
or a simplified version of the tr solution sugested by Sam Bisbee
或 Sam Bisbee 建议的 tr 解决方案的简化版本
tr ' ' \t < someFile > someFile
回答by John Millikin
Using Perl:
使用Perl:
perl -p -i -e 's/ /\t/g' file.txt
回答by DigitalRoss
Use the unexpand(1) program
使用 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'').
回答by Tarkin
better trcommand:
更好的tr命令:
tr [:blank:] \t
This will clean up the output of say, unzip -l, for further processing with grep, cut, etc.
这将清除unzip -l的输出,以便使用 grep、cut 等进行进一步处理。
e.g.,
例如,
unzip -l some-jars-and-textfiles.zip | tr [:blank:] \t | cut -f 5 | grep jar
回答by Ankur Agarwal
You can also use astyle
. I found it quite useful and it has several options too:
您也可以使用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.`
回答by mel
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'
回答by arkod
Example command for converting each .js file under the current dir to tabs (only leading spaces are converted):
将当前目录下的每个 .js 文件转换为制表符的示例命令(仅转换前导空格):
find . -name "*.js" -exec bash -c 'unexpand -t 4 --first-only "#!/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;
" > /tmp/totabbuff && mv /tmp/totabbuff "[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
"' {} \;
回答by daka
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.
从包含纯文本文件的文件夹中放置并执行脚本。
[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
回答by shrewmouse
If you are talking about replacing all consecutive spaces on a line with a tab then tr -s '[:blank:]' '\t'
.
如果您正在谈论用制表符替换一行上的所有连续空格,那么tr -s '[:blank:]' '\t'
.
T=$(printf "\t")
sed "s/[[:blank:]]\+/$T/g"
If you are talking about replacing all whitespace (e.g. space, tab, newline, etc.) then tr -s '[:space:]'
.
如果您正在谈论替换所有空格(例如空格、制表符、换行符等),那么tr -s '[:space:]'
.
sed "s/[[:space:]]\+/$T/g"
If you are talking about fixing a tab-damaged file then use expand
and unexpand
as mentioned in other answers.
如果您正在谈论修复制表符损坏的文件,请使用expand
和unexpand
如其他答案中所述。
回答by Tibor
Using sed:
使用sed:
##代码##or
或者
##代码##