I want to print the Nth column to the Mth column of a table; for example:

1 2 3 ... n ... m ...
X & $     #     !
@ $ %     (     *


n ... m 
#     !
(     *
  • 3
    How is the table?. Delimiter?. Example line(s)?. Example of extracted columns?. What have you tried?. – Isaac Jan 17 '18 at 2:19
  • I did not use the command until the end because I want the result to be only until the mth. – Johnny Tam Jan 17 '18 at 2:26

a perl one-liner

perl -slane 'print join " ", @F[$n-1 .. $m-1]' -- -n=5 -m=8 file
  • nice. i had completely forgotten that perl's -s option existed. that's odd - man perlrun says that the switches need to be before -- or any filename args, but -- means the end of perl's own options, so any -s options should come after the --. – cas Jan 17 '18 at 3:21
  • If I was using a script name instead of -e body it would be different – glenn jackman Jan 17 '18 at 12:58

Do you mean that you want to print between two columns such as from the 4th column to the 10th column?

If so:

cut -d ' ' -f 4-8 table.txt

That will print columns 4-8. cut will work well for this problem as long as the column delimiter is a single character.

Edit The cat wasn't necessary and I changed the delimiter to a space to fit your table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.