Wie ersetze ich Leerzeichen mit Tabulatoren in Linux in einer bestimmten Textdatei?
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'').
Ich denke, du kannst es mit awk versuchen
awk -v OFS="\t" '$1=$1' file1
oder SED, wenn Sie es vorziehen
sed 's/[:blank:]+/,/g' thefile.txt > the_modified_copy.txt
oder sogar tr
tr -s '\t' < thefile.txt | tr '\t' ' ' > the_modified_copy.txt
oder eine vereinfachte Version der von Sam Bisbee vorgeschlagenen tr-Lösung
tr ' ' \\t < someFile > someFile
Mit Perl:
Perl -p -i -e 's/ /\t/g' file.txt
besser tr Befehl:
tr [:blank:] \\t
Dadurch wird die Ausgabe von say, nzip -l für die weitere Verarbeitung mit grep, cut usw. aufgeräumt.
z.B.,
unzip -l some-jars-and-textfiles.Zip | tr [:blank:] \\t | cut -f 5 | grep jar
Laden Sie das folgende Skript herunter und führen Sie es aus, um Soft-Tabs rekursiv in Hard-Tabs in Nur-Text-Dateien zu konvertieren.
Platzieren Sie das Skript in dem Ordner, der die Nur-Text-Dateien enthält, und führen Sie es aus.
#!/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;
Beispielbefehl zum Konvertieren jeder .js-Datei unter dem aktuellen Verzeichnis in Registerkarten (nur führende Leerzeichen werden konvertiert):
find . -name "*.js" -exec bash -c 'unexpand -t 4 --first-only "$0" > /tmp/totabbuff && mv /tmp/totabbuff "$0"' {} \;
Sie können auch astyle
verwenden. Ich fand es sehr nützlich und es hat auch mehrere Optionen:
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.`
Dies ersetzt aufeinanderfolgende Leerzeichen durch ein Leerzeichen (jedoch nicht durch ein Tabulatorzeichen).
tr -cs '[:space:]'