将线条修剪到特定长度

将线条修剪到特定长度

我有一个包含很多行的文件,我想将每行的长度修剪为 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(née 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 个字符(^^零宽度断言意味着“行开始”)。如果该行太短,则不会返回任何内容。回来取决于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

相关内容