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.

I have a file such as the following:

1234
ABCD
EFGH

I'd like to convert it to the following:

2341
BCDA
FGHE

The actual file has 4,000 words, so I would like to do this in an efficient manner. I tried using the command cut -c 2-4,1 file.txt, but it produces the same exact output as the input. I was thinking I could use 3 different commands:

cut -c 1 file.txt > temp1.txt
cut -c 2-4 file.txt > temp2.txt
// combine the two with paste or pr

... but I would prefer a single command because I need to run it several times with slight modifications so running one command is less error prone than running 3 commands each time.

Is there any way to combine the 2 cut statements into one? Something like:

cut -c 1 file.txt | pr (cut -c 2-4 file.txt)

Or is there a better way to do this?

share|improve this question
add comment

7 Answers 7

up vote 6 down vote accepted

Using sed:

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

2341
BCDA
FGHE
share|improve this answer
add comment

With cut and paste you can also do an one-liner:

$ cat file
1234
ABCD
EFGH
$ paste --delimiter=''  <(cut file -c2-4) <(cut file -c1)
2341
BCDA
FGHE
share|improve this answer
    
+1, for the paste shortcut. It is easily understandable. :) –  Ankit Apr 16 '13 at 13:18
add comment

If you use bash, use the string indexing of parameter expansion:

while IFS= read -r word; do
  echo "${word:1:3}${word:0:1}"
done < file.txt
share|improve this answer
    
Thanks @Gilles. –  glenn jackman Sep 4 '12 at 16:43
add comment
$ cat test
1234
ABCD
EFGH
$ awk -F "" '{print $2$3$4$1}' test
2341
BCDA
FGHE

You can change the separator via "-F" according to your data, and arrange the order of fields arbitrarily.

share|improve this answer
add comment

Here's one way with perl:

perl -F'' -lane 'print @F[1..@F], $F[0]'

Auto-split at letter boundaries, rotate one to the left and print.

share|improve this answer
add comment

I found an alternative in a script way:

~$ cut -c2- file.txt>file2.txt
~$ cut -c1 file.txt>file3.txt
~$ paste -d "" file2.txt file3.txt>file4.txt
~$ rm file2.txt file3.txt

The script cuts the chains in a separate files. Then join into a new file (file4.txt) And finally removes the spare files.

the llua solution is more clean for my taste.

share|improve this answer
add comment

Have you tried rev ?

~$ cat filename | rev

share|improve this answer
1  
Take a look at the sample output. It's not a pure reverse, but good thinking. If you can't come up with a way to re-work this you should probably delete it, just so that it doesn't get downvoted and confuse the thread. –  slm Aug 23 '13 at 2:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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