Подтвердить что ты не робот

Заменить пробелы с вкладками в linux

Как заменить пробелы на вкладки в linux в текстовом файле?

4b9b3361

Ответ 1

Используйте программу нераспространения (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'').

Ответ 2

Я думаю, вы можете попробовать с 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

или упрощенная версия tr-решения, предложенная Сэмом Бисбием

tr ' ' \\t < someFile > someFile

Ответ 3

Использование Perl:

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

Ответ 4

лучше команда tr:

tr [:blank:] \\t

Это приведет к очистке вывода, например, unzip -l, для дальнейшей обработки с помощью grep, cut и т.д.

например.

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

Ответ 5

Пример команды для преобразования каждого .js файла под текущим директором в вкладки (только ведущие пробелы преобразуются):

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

Ответ 6

Загрузите и запустите следующий script, чтобы рекурсивно преобразовать мягкие вкладки на жесткие вкладки в текстовых файлах.

Разместите и выполните script внутри папки, содержащей текстовые файлы.

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

Ответ 7

Вы также можете использовать 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.`

Ответ 8

Это заменит последовательные пробелы одним пробелом (но не вкладкой).

    tr -cs '[:space:]'