Print limited characters per a line

Saturday, December 29, 2012 , , , , 2 Comments

Printing first 80 characters in a line.Below are the different ways to do it.
Cut
cut -c1-80 your_file
Awk
awk '{print substr($0,0,80)}' your_file
Sed
sed -e 's/^\(.\{80\}\).*/\1/' your_file
Perl
perl -lne 'print substr($_,0,80)' your_file
perl -lpe 's/.{80}\K.*//s' your_file

Grep
grep -o "^.\{80\}" your_file

2 comments:

  1. That's all fine and dandy, but I once attempted to write a script to do the same thing (wrap the text, splitting using whitespace, independent of terminal width... basically a fold command). That's a piece of cake...

    The catch? I want to do the same thing but with text containing control characters and hidden terminal formatting codes, escape characters (are those part of control? dunno), etc.

    The script turned out to be a nightmare to write. I finally got one working but it's useless because it's WAY too slow. Too many calculations...

    Care to tackle the topic?

    — Yuri

    ReplyDelete
  2. That's worth a thought. Surely will try it out some time.

    ReplyDelete