Help editing a file

I have a data file which is a flat text file that has certain characters that are at the beginning of each line that identify it as being a certain type of data element. I want to write a small shell script that will take my data file and look at the beginning of certain lines and if it finds this certain line, then make a change to information that is located at different positions within the line. I know the exact positions of the data that I want to change. For example... I have lines that begin with the following:

AA0GAA11-030 or
BA011BDSXH or
CA0 or
DA00100196800018184

where the AA0, or BA0, or CA0, or DA0 are the first three characters of the line that are constant.

The data that follows later on within the line is a date 1902. I want to change the date from 1902 to 2002. So in essence I want to search my data file for lines that begin with AA0* or BA0* or CA0* or DA0* and once I encounter this pattern, I want to then look at a certain position in the line and change the data 1902 to 2002.

Can anyone help with this?

Try:
sed '/^[A-D]A0/s/1902/2002/' < olddata > newdata

I tried using your suggestion, however, the date 1902 is not in a seperate field. It is embedded along with other data. For example:

AA0GAA11-030 ASY960000336ABDUL BARI MD 618 5TH AVENUE CAIRO GA31728 BARBARA GROOMS 229377709019020516053400 C00200 PROD

This is one line in the data file. The line begins with AA0 and the date is actually further down in a string of numbers. Is there some "wild-card" that needs to be added so that it finds the 1902 within the string of data?

I just tried that sed command again using your data and it worked.

I tried it again as well on my origional data file and it doesn't change the date.....however.... it appears that when we post data on here that blank spaces and nulls are often removed so that the data string looks like one contiguous string. I copied and pasted the data that was in my origional post and yes indeed, the command worked as expected... just seems that when I run the command on my data file... that contains.. nulls and spaces... it doesn't work.. I will keep trying... thanks for your help.. if you think of something I'd appreciate your help alot.

Spaces are not a problem. All we need is for the line to start with one of the correct sequences and then to have 1902 anywhere in the line as a contiguous sequence. By using quoting I was able to recover your spaces and produced your original line. It is so long that I inserted backslash/newlines combos to shorten it for posting:

AA0GAA11-030                ASY960000336ABDUL BARI MD                    618 5TH AVENUE\
                                              CAIRO               GA31728         BARBARA\
 GROOMS                   229377709019020516053400                C00200     PROD  

But I tested it all joined together as a single long line. That also worked. Nulls are another matter. I have no way to recover any nulls in your line. And the presence of nulls indicates that we do not have a text file. Sed may indeed fail if you really have nulls in the line. Ditto for any other text processing tool. If you have binary data to edit a c program my be your only option. Send of of your lines through "od -c" and post the result so we can see what we are actually dealing with. Maybe someone will have an idea...

Good news... ! the command appears to be working correctly... however... if there is more than one instance of the date in each line.. it only changes the first date.. and skips any subsequent dates that might be later on in the data string for that particular line...

What changed?

To handle multiple dates....
sed '/[1]A0/s/1902/2002/g' < olddata > newdata


  1. A-D ↩︎

WOW!!! =) That command works like a charm... =) Thanks alot guys for your help =)