I would like to cat file, but with limit to max length to each line. For example I a have file with 10 lines each have 10000 characters and I would like to print first 100 characters from each line. Is something like that possible with cat or some alternative? thx.
32 Answers
With the cut tool you can limit the output to 100. Since you only interested in the characters hence the columns they occupy this should do that nicely:
cut -c-100 fileIn case you wish to remove the spaces in there this would help:
sed 's/ //g' file | cut -c-100See: man cut
Using awk:
awk '{ print substr( $0, 0, 100 ) }' fileGetting rid of spaces again if required:
awk '{ gsub (" ", "", $0); print substr( $0, 0, 100 ) }' fileAWK:
gsub (" ", "", $0): find " "(spaces) and replace with "" globally in target string $0.
substr( $0, 0, 100 ): it returns 100 number of chars from string $0, starting at position 0.
Shell
bash and ksh way to achieve this would be:
while IFS= read -r line || [ -n "$line" ];do printf "%s\n" "${line:0:100}"; done < input.txtHere we take advantage of parameter expansion in form ${parameter:start:offset} to print from starting point till offset. Should be noted that this isn't specified by POSIX and doesn't work in dash ( the default /bin/sh on Ubuntu).
Perl
perl -ne 'printf "%s\n",substr($_,0,100)' input.txtThis takes advantage of built-in substr function.
Python
python is Python 2.7 on Ubuntu:
python -c 'import sys; print "\n".join(map(lambda x: x[:100],sys.stdin.readlines()))' < input.txtFor python3, enclose arguments to print with braces:
python3 -c 'import sys; print("\n".join(map(lambda x: x[:100],sys.stdin.readlines())))' < input.txtThis takes advantage of shell's redirection for file's contents into python's stdin stream, then reads all lines into list from the stdin,. The map() function allows us to process that list via lambda x: x[:100], obtaining a new list, where each item is a slice of each corresponding line, 100 characters long. "\n".join() lets us combine the list of strings into one again with newline as separator.