Solved

Selective Replace of characters

Posted on 2009-07-13
1
360 Views
Last Modified: 2012-06-27
I have a file where each line represents a database record. Each field is fixed width.
I need to add a "prefix" to one of the numeric fields. So, in one file, all the records will be added by 1000, on a second file by 2000, etc (I know for a fact that the numbers have less digits than the amount I'm adding).

I've made many manipulations with the file already using sed. But can't seem to find the way to do this one. For instance, suppose the lines are of the form shown below. I'm trying to change the 10th char to my prefix and then need to replace all consecutive spaces after that to 0. It's this replacement I'm having trouble with.

I know how to identify the fields I want:
sed 's/^\(.\{9\}\) \([ ]*\)\(.*\)$/\11\2\3/g'  #doesn't work because it doesn't replace spaces, just the prefix
I know how to replace spaces with 0:
sed 's/ /0/g'   #which doesn't work either as it replaces all spaces and not just the ones I want.

I've done quite a few manipulations with these files using sed but can't seem to do this one. I'm probably just being dense, and can't think out of the box right now. Anybody's got any suggestions? (It doesn't need to use sed at all, if there's a better alternative)

Sample text:
 
Field01     2     3Field04
Field01    23     5Field04
Field01     5     3Field04
Field01     2     4Field04
 
should become
 
Field01  1002     3Field04
Field01  1023     5Field04
Field01  1005     3Field04
Field01  1002     4Field04

Open in new window

0
Comment
Question by:pauloaguia
[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
1 Comment
 
LVL 9

Accepted Solution

by:
pauloaguia earned 0 total points
ID: 24839370
I managed to get an answer on my own. For this sample, the command would be something like I show below.

This is actually a loop. It starts by creating a label a (the :a part). This will be used to create a loop later on.

On the next line, it matches any 9 characters, another character (the one I want to replace with the prefix) an undetermined number of nonspace characters (the resulting numbers will have 3 more digits after the first one) followed by a space and another undetermined number of characters (at least 13, to make sure it won't start matching with the spaces after the number).
The substitution is rather simple - everything is maintained except for the prefix character and the first space after it that gets turned into a 0.

Finally, the 'ta' part, means that if a replacement as occurred, then the program should jump to the label :a. Thus the loop is created.

To understand this better, I also added what one of the lines would look like after each iteration...
sed -e :a -e 's/\(.\{9\}.\([^ ]\{0,3\}\) \(.\{13,\}\)/\11\20\3/g;ta'
 
 
iterations for the first line:
it1: Field01  10 2     3Field04
it2: Field01  1002     3Field04
it3: No match. Don't jump back to :a

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

729 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