[英]Replace whitespaces with tabs in linux
如何在給定的文本文件中用 linux 中的制表符替換空格?
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
使用Perl :
perl -p -i -e 's/ /\t/g' file.txt
更好的tr命令:
tr [:blank:] \\t
這將清除unzip -l的輸出,以便使用 grep、cut 等進行進一步處理。
例如,
unzip -l some-jars-and-textfiles.zip | tr [:blank:] \\t | cut -f 5 | grep jar
下載並運行以下腳本,以遞歸方式將純文本文件中的軟標簽轉換為硬標簽。
從包含純文本文件的文件夾中放置並執行腳本。
#!/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;
將當前目錄下的每個 .js 文件轉換為制表符的示例命令(僅轉換前導空格):
find . -name "*.js" -exec bash -c 'unexpand -t 4 --first-only "$0" > /tmp/totabbuff && mv /tmp/totabbuff "$0"' {} \;
您也可以使用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
。
使用sed :
T=$(printf "\t")
sed "s/[[:blank:]]\+/$T/g"
要么
sed "s/[[:space:]]\+/$T/g"
這將用一個空格(但不是制表符)替換連續的空格。
tr -s '[:blank:]'
這將用制表符替換連續的空格。
tr -s '[:blank:]' '\t'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.