How to print first 'n' columns?

Hello All,

I have a file of 1000 columns. I need to print only the first 100 columns in to a new file. I have tried with the following code

awk -F  '{ for(i=0; i<=100; i++) {printf $i}' inputfile > outputfile 

But this is not working, I need the output file also of the same format of the input, but with out other columns.

Can anyone help me in this regard?

Expecting your reply and thanks in advance.

Regards
Fredrick.

file=/path/to/file
delimiter=' '
cut -d "$delimiter" -f 1-100 "$file"

Try something like

cut -d'*' -f1-100 inputfile > outputfile

where * = your field separator.

Thank you very much "cfajohnson", the following code is working perfect.

file=/path/to/file
delimiter=' '
cut -d "$delimiter" -f 1-100 "$file"

---------- Post updated at 11:22 AM ---------- Previous update was at 11:21 AM ----------

Thank you "Vi-Curious", it working fine.

The following is the right awk statement in your way of doing things,

awk -F' '  '{ for(i=1; i<=2; i++) {print $i} }' inputfile > outputfile
  • -F without option !
  • missing brace...
  • "print $0" is meant to print whole line.

Everything else is fine.