Move text in a unix flat file

Posted on 2006-11-22
Last Modified: 2013-12-26
I've got a file of IDs that basically looks as follows :


and I want to edit that file (into another file) and swap the data elements around so that it looks like :


(ie 5 char code followed by 9 char code)

How can I do that in one statement?  I managed to use cut so that that I could get the 9 chars :

cat file_in | cut -c0-9 > file_out

but that's not enough.  Do I need a sed statement?
Question by:JoHarris
  • 4
LVL 11

Accepted Solution

cjjclifford earned 250 total points
ID: 17995248
you don't even need the cut, a simple "sed" will do it...

sed 's/^\(.....\)\(.........\)/\2\1/' file_in > file_out

Note this is hardcoded to 5 leading characters, and 9 trailing characters...

LVL 11

Expert Comment

ID: 17995255
That will transpose the first 14 characters, leaving anything else in the line in-place. If this is not desirable then extend the regex:

sed  's/^\(.....\)\(.........\).*$/\2\1/'

Also, you may want to search for matches that are strictly characters ([A-Z]) or strictly numerics ([0-9]) rather than any 5/9 characters.
LVL 11

Expert Comment

ID: 17995292
Also, using bounds may be an easier way of argumenting the number of characters...


using bounds, and strictly characters/numbers, stripping any suplurfous characters after first 14:

sed 's/^\([A-Z]\{5\}\)\([0-9]\{9\}\).*$/\2\1/' in_file > out_file


$ echo ABCDE123456789extra | sed 's/^\([A-Z]\{5\}\)\([0-9]\{9\}\).*$/\2\1/'


Author Comment

ID: 17995325
Thats great thanks - the flat file will always contain the 9 followed by the 5 so the 1st one works a treat.

Thanks for helping me out so quickly!
LVL 11

Expert Comment

ID: 17995347

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how do i create updater to My Activex application? 3 84
Macro to save highlighted rows as a separate file 5 43
sameEnds challenge 3 156
bigDiff challenge 17 104
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now