특정 길이로 선 자르기

특정 길이로 선 자르기

줄이 많은 파일이 있고 각 줄의 길이를 80자로 자르고 싶습니다. 어떻게 해야 하나요?

80자보다 짧은 줄을 필터링했으므로 이제 80자보다 긴 줄이 있는 파일이 남았습니다. 모든 줄이 정확히 80자가 되도록 각 줄을 자르고 싶습니다. 즉, 각 줄의 처음 80자를 유지하고 나머지 줄을 삭제하고 싶습니다.

답변1

cut다음 명령을 사용할 수 있습니다 .

cut -c -80 file

그리고 grep:

grep -Eo '.{80}' file

답변2

사용AWK:

awk '{print substr($0,1,80)}' file.txt

사용자르다:

 cut -c -80 file.txt

사용콜름:

colrm 81 file.txt

사용sed:

sed 's/^\(.\{80\}\).*$/\1/' file.txt

사용그렙:

grep -Eo '.{80}' file.txt

답변3

파일의 각 줄을 잘라내고 현재 콘솔에서 인쇄하려면 다음을 사용하세요.

cut -c -80 infile               # cut only counts bytes (fail with utf8)
grep -o '^.\{1,80\}' infile
sed 's/\(^.\{1,80\}\).*/\1/' infile

80번째 문자에 개행 문자를 삽입하고 80자를 초과하는 각 줄을 더 많은 줄로 분할하려면 다음을 사용하세요.

fold -w 80 infile            # fold, like cut, counts bytes.

공백(전체 단어)에서만 분할하려면 다음을 사용하세요.

fold -sw 80 infile

>outfile위의 모든 해결 방법에 대해 명령 끝에서 다른 파일로 리디렉션하여(동일한 이름을 사용하지 마십시오. 작동하지 않음) 결과를 outfile.

fold -sw 80 infile > outfile

답변4

Raku 사용(옛 Perl6)

~$ raku -ne 'put ~$0 if m/ ^^(. ** 80) /;'

산출:

the of and to in a is that for it as was with be by on not he i this are or his
the of and to in a is that for it as was with be by on not he i this are or his
the of and to in a is that for it as was with be by on not he i this are or his
the of and to in a is that for it as was with be by on not he i this are or his
[TRUNCATED]

위의 코드는 줄의 처음 80자를 반환합니다( ^^너비가 0인 어설션은 "줄의 시작"을 의미함). 줄이 너무 짧으면 아무것도 반환되지 않습니다. 반품에 따라80자, 형식을 사용하세요 ** 1..80.

캡처된 숫자는 로 시작합니다 $0. 캡처 변수에 추가하여 .chars반환된 문자 수를 읽습니다 ~$0.

~$ raku -ne 'put ~$0.chars if m/ ^^(. ** 80) /;' ~/top50.txt
80
80
80
80
[TRUNCATED]

HTH.

https://raku.org

관련 정보