The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 01-31-2008
stresing stresing is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 3
Finding nth occurrence in line and replacing it

Hi,

I have several files with data that have to be imported to a database. These files contain records with separator characters. Some records are corrupt (2 separators are missing) and I need to correct them prior to importing them into the db.
Example:

|field1|field2|field3|field4|field5|field6|field7|field8|field9|field10|field11|field12|||
|field1|field2|field3|field4|field5|field6|field7|field8|field9|field10|field11|field12|field13|fiel d14|field15
|field1|field2|field3|field4|field5|field6|field7|field8|field9|field10|field11|field12|field13

The records 1 and 2 are correct. They consist of 15 optional fields separated by 15 | characters.
Record 3 is corrupt since it's missing 2 separator characters ahead field13.
It should look like:
|field1|field2|field3|field4|field5|field6|field7|field8|field9|field10|field11|field12|||field13

How can I achieve this by using sed or awk or something else?
Any help is greatly appreciated!

TIA,
Stephan.