Help with "cut" command in Unix

Hi! Just a basic question:

I want to use the "cut" command to get some columns from a file, like this:

cat myfile | cut -f 5,3,2,1

The problem is that the output contains the columns I want, but in increasing order: 1,2,3,5, and not in the order I set before. I know I could also use awk, but for my shell script I do prefer to use "cut". Is there a way to solve this?

Thanks!!

cut processes the file from left to right, it does not "go back", if you are on position 5, then you cannot go back to 1. use awk.

1 Like

while read -a L
do
for i in 4 2 1 0 # indexes begin at 0
do echo -n ${L[$i]}
done
echo
done <myfile

or
while read -a L
do echo "${L[4]} ${L[2]} ${L[1]} ${L[0]}"
done <myfile

1 Like