I want to output a range of columns from a file, where the range is set by two variables.

Really struggling with this. I imagine cut is the best tool, but as far as I'm aware it doesn't accept variables to designate the range.

It's a tab delimited file. I've tried:


awk -F $'\t' -v c=$Column '{ for (x=1; x<=c; x++) {  printf "%s\n", $x } }' ~/file.txt > ~/file1.txt

But that doesn't work (aim being to select the column range of 1 to 8401).

To give an example of Input and Output:



12   33   55   23   13   88

Using the $column variable I would output $ColumnStart to $ColumnEnd


12   33   55

Use the variables like this:

cut -f${ColumnStart}-${ColumnEnd}

(assuming the variables always contain appropriate values...).

| improve this answer | |
  • Well I'll be a monkeys uncle! Brilliant, thank you. I've used similar methods to separate variables from things, but didn't even try it in cut because i spoke to a number of people that said cut wouldn't except a variable unless its input being piped into it! – Giles Dec 22 '16 at 17:58
  • 2
    It works because the variable is expanded by the shell before the argument is passed to cut. – DopeGhoti Dec 22 '16 at 18:04

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.