
줄이 많은 파일이 있는데 각 줄의 길이를 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.