The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
editing ELF file tejuwala Linux 1 05-17-2008 08:22 PM
Editing file rahul303 Shell Programming and Scripting 4 09-23-2007 07:49 PM
Editing File using awk/sed Mohammed Shell Programming and Scripting 4 05-16-2007 08:00 AM
Editing the File using Awk awk_beginner Shell Programming and Scripting 3 04-06-2007 07:43 AM
Help with editing file dsravan Shell Programming and Scripting 4 01-31-2007 09:59 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-21-2002
Registered User
 

Join Date: Apr 2002
Location: Florida
Posts: 44
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?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-21-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,355
Try:
sed '/^[A-D]A0/s/1902/2002/' < olddata > newdata
Reply With Quote
  #3 (permalink)  
Old 05-21-2002
Registered User
 

Join Date: Apr 2002
Location: Florida
Posts: 44
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?
Reply With Quote
  #4 (permalink)  
Old 05-22-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,355
I just tried that sed command again using your data and it worked.
Reply With Quote
  #5 (permalink)  
Old 05-22-2002
Registered User
 

Join Date: Apr 2002
Location: Florida
Posts: 44
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.
Reply With Quote
  #6 (permalink)  
Old 05-22-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,355
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:

Code:
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...
Reply With Quote
  #7 (permalink)  
Old 05-22-2002
Registered User
 

Join Date: Apr 2002
Location: Florida
Posts: 44
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...
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0