Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Move text in a unix flat file

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

123456789AAAAA
123456790AAAAA
123456791AAAAA

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

AAAAA123456789
AAAAA123456790
AAAAA123456791

(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?
0
Comment
Question by:JoHarris
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 11

Accepted Solution

by:
cjjclifford earned 1000 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...

0
 
LVL 11

Expert Comment

by:cjjclifford
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.
0
 
LVL 11

Expert Comment

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

so...

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


e.g.

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

0
 

Author Comment

by:JoHarris
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!
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 17995347
:-)
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

722 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