可能重复:
截断stdin行长度?
我一直在寻找一个awk或perl(或者可能sed?)单行来打印一行中的前80个字符用作:
cat myfile.txt | # awk/perl here
Run Code Online (Sandbox Code Playgroud)
我猜想perl -pe 'print $_[0..80]'应该有所作为,但我对perl并不擅长.
编辑 perl -pe 'print $_[0..80]不起作用,我不知道为什么.这就是我问这个问题的原因.我想在所有那些沉默的downvotes之后解释..
也cat myfile.txt只是为了证明命令应该在管道中,我实际上正在使用其他一些输出.
Vij*_*jay 13
切:
cut -c1-80 your_file
Run Code Online (Sandbox Code Playgroud)
AWK:
awk '{print substr($0,0,80)}' your_file
Run Code Online (Sandbox Code Playgroud)
SED:
sed -e 's/^\(.\{80\}\).*/\1/' your_file
Run Code Online (Sandbox Code Playgroud)
perl的:
perl -lne 'print substr($_,0,80)' your_file
Run Code Online (Sandbox Code Playgroud)
要么:
perl -lpe 's/.{80}\K.*//s' your_file
Run Code Online (Sandbox Code Playgroud)
grep的:
grep -o "^.\{80\}" your_file
Run Code Online (Sandbox Code Playgroud)