Help editing a file

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help editing a file
# 1  
Old 05-21-2002
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
CA0 or

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?
# 2  
Old 05-21-2002
sed '/^[A-D]A0/s/1902/2002/' < olddata > newdata
# 3  
Old 05-21-2002
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?
# 4  
Old 05-22-2002
I just tried that sed command again using your data and it worked.
# 5  
Old 05-22-2002
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.
# 6  
Old 05-22-2002
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...
# 7  
Old 05-22-2002
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...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert vi editing to text editing

Dear Guru's I'm using Putty and want to edit a file. I know we generally use vi editor to do it. As I'm not good in using vi editor, I want to convert the vi into something like text pad. Is there any option in Putty to do the same ? Thanks for your response. Srini (6 Replies)
Discussion started by: thummi9090
6 Replies

2. UNIX for Dummies Questions & Answers

File editing

Hi All, I've to edit the below file in the Current File: XXX Rows found with null for one or more non-nullable columns in the source table SELECT COUNT(*) FROM ( SELECT 1 as one FROM datum WHERE:wall: datum.ID IS NULL ) a (1 Reply)
Discussion started by: udayakumar
1 Replies

3. Shell Programming and Scripting

Need help in editing a file

I have a file which has 10 million records in it. When am trying to edit the file with vi, the following error occurs: ~ ~ ~ ~ ~ ~ ~ ~ "file1" Value too large for defined data type Is there any way that I can edit this file without using vi? Any help would be really appreciated.... (8 Replies)
Discussion started by: bobby1015
8 Replies

4. Shell Programming and Scripting

editing line in text file adding number to value in file

I have a text file that has data like: Data "12345#22" Fred ID 12345 Age 45 Wilma Dino Data "123#22" Tarzan ID 123 Age 33 Jane I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes: Data "1012345#22" Fred ID... (16 Replies)
Discussion started by: say170
16 Replies

5. Shell Programming and Scripting

Help with file editing while keeping file format intact

Hi, I am having a file which is fix length and comma seperated. And I want to replace values for one column. I am reading file line by line in variable $LINE and then replacing the string. Problem is after changing value and writing new file temp5.txt, formating of original file is getting... (8 Replies)
Discussion started by: Mruda
8 Replies

6. Shell Programming and Scripting

file editing

how to remove duplicate word in a file ? (2 Replies)
Discussion started by: mail2sant
2 Replies

7. Shell Programming and Scripting

Editing a File

Hi all, I have a file with following contents # rad124 # radkus # raddebug # radtrace I could like to remove the # and space present before the key word "rad". Any ways to do this using "subsitution method(:%s/old/new/g)" will be hepful. (1 Reply)
Discussion started by: ramkriz
1 Replies

8. Linux

file editing

I have created a file with vi -x (file name) this is encrypted file when i again open this file it ask me to enter a password before editing it.Can i remove this password but i don't want to delete a file how to do this. Thanks (0 Replies)
Discussion started by: ambavaram
0 Replies

9. Shell Programming and Scripting

Editing file

Hi, I am in a situation wherein am getting file file certailn values suppose 1u56979hhghhklklkkkjkjkjk 0 0 0 The file will have values like above only. I need to add another field of NULL value(of length 9) at the end of first column i.e. It should like this after editing:... (4 Replies)
Discussion started by: rahul303
4 Replies

10. UNIX for Advanced & Expert Users

Editing the end of the file without loading the entire file

hi! I am a newbee. I would really appreciate if you can answer the following question: I have a huge data file, 214MB with several coloumns. I need to delete the very last line of the file. Everything I know takes a lot of time to do it ( because I have to open the file in an editor or run a... (3 Replies)
Discussion started by: Garuda
3 Replies
Login or Register to Ask a Question