Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Possible Duplicate:
Combining 2 different cut outputs in a single command?

I have One file - temp.txt - which contains

ABCDF

PQRST

LMNOP

I tried this cut command

$ cut -c 2-5 temp.txt
BCDF

QRST

MNOP

output is right.

$ cut -c 1,2,3,4 temp.txt
ABCD

PQRS

LMNO

output is right.

But when I change sequence, meaning

$ cut -c 2-4,1 temp.txt
ABCD

PQRS

LMNO

above output displayed.

but logically output is

BCDA

QRSP

MNOL

so my question is how this happen ,any reason behind that.

share|improve this question

marked as duplicate by Gilles, jasonwryan, Renan, warl0ck, Stéphane Gimenez Sep 17 '12 at 19:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
I think that cut CANNOT mix chars! You have to do a simple "hack" like this: echo -n ${string:1:3}; echo ${string:0:1} but you have to first grep you string :) –  polslinux Sep 4 '12 at 6:25

2 Answers 2

up vote 1 down vote accepted

The explanation is in man cut, though it is easy to read it as your preferred expectation. However it does say:

Selected input is written in the same order that it is read  

It is perhaps ambiguous as to what is meant by input, but typically Options are just that, options, whereas the file being processes is definitely input.

The list, eg.-c 2,3,4,1 is very much like the regex list: [2341]; It means any of the numbers in the list, even if they are doubled up.. eg. -c 2,2,2,2,2,2 is the same as -c 2 .. and for the regex, [2222222] is the same as [2]... So the order of your options is not relevant.

share|improve this answer

The solution is:

sed 's:^\(.\)\(.*\):\2\1:' file.txt

(someone published this answer to a similar question yesterday)

Cut seems not to order the output, only select ranges of columns/fields and output in the same order as they were.

share|improve this answer

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