The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
stop unix find on a directory structure after finding 1st occurrence jm0221 Shell Programming and Scripting 3 06-06-2008 07:19 PM
help with finding & replacing pattern in a file dusoo Shell Programming and Scripting 0 05-23-2008 03:59 AM
replacing certain characters with new line? Bashar Shell Programming and Scripting 4 05-13-2007 04:34 PM
finding & replacing blank rows/spaces in a file Gerry405 SUN Solaris 2 07-21-2005 04:49 AM
shellscript for finding and replacing in DG-UNIX Gerry405 UNIX for Dummies Questions & Answers 3 07-12-2005 08:12 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #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.
  #2 (permalink)  
Old 01-31-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
ok - not going to win a prize for pretty code, but,

Code:
 awk -F"|" 'NF==16{print}NF==14{OFS="|";$16=$14;$14="";print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16}' infile
works. With the input you provided, the following is the o/p:

Code:
|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
HTH,
  #3 (permalink)  
Old 01-31-2008
stresing stresing is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 3
Hey, thanks for your fast reply!

I don't care about pretty solutions - it works and that's important to me! I just tested on my real file and that's what I looked for!

Maybe one day I have the time to pimp the statement - but I won't win a prize as well...

Thanks again!
  #4 (permalink)  
Old 01-31-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
If that's the requirement:

Code:
awk 'NF==16||$NF=FS FS $NF' OFS="|" FS="|" filename
Use nawk or /usr/xpg4/bin/awk on Solaris.
  #5 (permalink)  
Old 01-31-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
now there you go - that's what a real guru comes up with, and I'm impressed - never even thought of that sneaky solution.

nice piece of code radoulov - I keep learning something new from your posts - you da man.
  #6 (permalink)  
Old 02-01-2008
stresing stresing is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 3
Wow, that's a really nice solution. I never would have found that on my own - of course, that's the reason why I post to the Dummies forum... Thank you!
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:07 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0