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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 58
isEverywhere  challenge 19 84
wordmultiple challenge 12 142
Is there a simple front-end menu system. 9 105
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 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