特定の長さに線を切る

特定の長さに線を切る

行が多いファイルがあり、各行の長さを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

関連情報