Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!uwvax!dogie.macc.wisc.edu!indri!aplcen!bink From: bink@aplcen.apl.jhu.edu (Ubben Greg) Newsgroups: comp.sources.wanted Subject: Re: Column shifting Summary: Use SED (as always) Message-ID: <1287@aplcen.apl.jhu.edu> Date: 20 May 89 09:16:16 GMT References: <68200001@sts> Reply-To: bink@aplcen.apl.jhu.edu (Greg Ubben) Organization: The Johns Hopkins University, Baltimore MD Lines: 36 In article <68200001@sts> roy@sts.sts.UUCP 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. Sed (the stream editor) does this very nicely: > Some examples are: > > input file is: 0123456789 > > Move column 7 to column 1: 0712345689 sed "s,\(.\)\(......\)\(.\),\1\3\2," > Move column 7 to column 1, column 3 to column 5: 0712453689 sed "s,\(.\)\(..\)\(.\)\(..\)\(.\)\(.\),\1\6\2\4\3\5," > Move column 7 to column 1, column 3 to column 8: 0712456839 sed "s,\(.\)\(..\)\(.\)\(...\)\(.\)\(.\),\1\5\2\4\6\3," This could be done using CUT and PASTE, but it is longer, messier, and slower: > Move column 7 to column 1, column 3 to column 5: 0712453689 tmp=/tmp/shift$$ trap "rm -f ${tmp}?; trap 0; exit" 0 1 2 15 cut -c1,8 $file >${tmp}a cut -c2-3,5-6 $file >${tmp}b cut -c4,7,9- $file | paste -d\\0 ${tmp}? - (3 more processes and 3 more files/pipes needed.) -- Greg Ubben bink@aplcen.apl.jhu.edu