반응형
Linux에서 공백을 탭으로 바꾸기
주어진 텍스트 파일에서 Linux의 공백을 탭으로 어떻게 대체합니까?
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'').
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 명령 :
tr [:blank:] \\t
이것은 grep, cut 등으로 추가 처리를 위해 say, unzip -l 의 출력을 정리합니다 .
예 :
unzip -l some-jars-and-textfiles.zip | tr [:blank:] \\t | cut -f 5 | grep jar
Perl 사용 :
perl -p -i -e 's/ /\t/g' file.txt
다음 스크립트를 다운로드하고 실행하여 소프트 탭을 일반 텍스트 파일의 하드 탭으로 재귀 적으로 변환합니다.
일반 텍스트 파일이 포함 된 폴더 내에서 스크립트를 배치하고 실행합니다.
#!/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;
현재 dir 아래의 각 .js 파일을 탭으로 변환하는 예제 명령 (선행 공백 만 변환 됨) :
find . -name "*.js" -exec bash -c 'unexpand -t 4 --first-only "$0" > /tmp/totabbuff && mv /tmp/totabbuff "$0"' {} \;
You can also use astyle
. I found it quite useful and it has several options too:
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.`
This will replace consecutive spaces with one space (but not tab).
tr -cs '[:space:]'
참고URL : https://stackoverflow.com/questions/1424126/replace-whitespaces-with-tabs-in-linux
반응형
'IT story' 카테고리의 다른 글
SQLite 데이터베이스에 대한 테이블의 열 목록을 어떻게 얻을 수 있습니까? (0) | 2020.08.31 |
---|---|
들어오는 모든 연결을 허용하도록 PostgreSQL을 구성하는 방법 (0) | 2020.08.31 |
'반응 스크립트 시작'명령은 정확히 무엇입니까? (0) | 2020.08.30 |
Java 호출 스택의 최대 깊이는 얼마입니까? (0) | 2020.08.30 |
POSIX 비동기 I / O (AIO)의 상태는 어떻습니까? (0) | 2020.08.30 |