[comp.sources.d] Column shifting

roy@sts.sts.UUCP (05/20/89)

I'm wondering if there's been a program written that takes a file and
outputs that same file except with arbitrary columns shifted over.
Some examples are:

input file is: 0123456789

Move column 7 to column 1: 0712345689

Move column 7 to column 1, column 3 to column 5: 0712453689

Move column 7 to column 1, column 3 to column 8: 0712456839

This sounds a bit like the bubble sort, so I think it probably exists.
Just out of curiosity, are there any other algorithms out there?
Thanks.

==================================================================
Roy Bixler                   | UUCP: ...!{claris,sun}!sts!roy    |
Semiconductor Test Solutions | Internet: sts!roy@Claris.COM      |
4101 Burton Dr.              | phones: (408) 727-2885 x132 (work)|
Santa Clara, CA  95054	     |         (408) 289-1035      (home)|
==================================================================

roy@sts.sts.UUCP (05/20/89)

/* Written 10:45 am  May 19, 1989 by roy@sts.sts.UUCP in sts:comp.sources.d */
/* ---------- "Column shifting" ---------- */
I'm wondering if there's been a program written that takes a file and
outputs that same file except with arbitrary columns shifted over.
Some examples are:

input file is: 0123456789

Move column 7 to column 1: 0712345689

Move column 7 to column 1, column 3 to column 5: 0712453689

Move column 7 to column 1, column 3 to column 8: 0712456839

This sounds a bit like the bubble sort, so I think it probably exists.
Just out of curiosity, are there any other algorithms out there?
Thanks.
/* End of text from sts:comp.sources.d */

Somebody just suggested using the "cut" command of SysV as the
solution to this.  I read the documentation and it looks like it
merely deletes certain columns or fields, but does not move them as I
wished.  Am I wrong about "cut"?  Any other ideas?

==================================================================
Roy Bixler                   | UUCP: ...!{claris,sun}!sts!roy    |
Semiconductor Test Solutions | Internet: sts!roy@Claris.COM      |
4101 Burton Dr.              | phones: (408) 727-2885 x132 (work)|
Santa Clara, CA  95054	     |         (408) 289-1035      (home)|
==================================================================

charles@hpcvca.CV.HP.COM (Charles Brown) (05/27/89)

> I'm wondering if there's been a program written that takes a file and
> outputs that same file except with arbitrary columns shifted over.
> Some examples are:
> 
> input file is: 0123456789
> 
> Move column 7 to column 1: 0712345689

Try:
 sed -e "s/^\(.\)\(.....\)\(.\)/\1\3\2/"
--
	Charles Brown	charles@cv.hp.com or charles%hpcvca@hplabs.hp.com
			or hplabs!hpcvca!charles or "Hey you!"
	Not representing my employer.

bill@twwells.uucp (T. William Wells) (05/28/89)

In article <4310004@hpcvca.CV.HP.COM> charles@hpcvca.CV.HP.COM (Charles Brown) writes:
: > I'm wondering if there's been a program written that takes a file and
: > outputs that same file except with arbitrary columns shifted over.
: > Some examples are:
: >
: > input file is: 0123456789
: >
: > Move column 7 to column 1: 0712345689
:
: Try:
:  sed -e "s/^\(.\)\(.....\)\(.\)/\1\3\2/"

Watch out for this: regular expressions for sed containing long
strings of `.'s seem to be able to overflow internal buffers or
something and sed doesn't tell you that it has failed. I've seen this
happen on SunOS (3.2, I think) and on SysV/386 3.0.

I've also seen this happen with egrep.

(No, I don't have specifics; each time this has happened, I've been
too busy to nail down the problem.)

One other option is to use cut(1) and paste(1).

Some people might suggest awk, but if efficiency is a concern, skip
it. Awk, at least as provided as a command with the UNIXs I've used,
is amazing inefficient.

(And all of this assumes UNIX. Let's hope we're right.)

---
Bill                            { uunet | novavax } !twwells!bill