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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
copy file to a directory by sequence fooky UNIX for Dummies Questions & Answers 4 11-24-2007 10:40 PM
adding string to a file andy2000 Shell Programming and Scripting 8 04-17-2007 04:29 AM
Catpuring the last entry of a repetetive sequence from a file! jobbyjoseph SUN Solaris 4 11-15-2006 11:59 AM
How to extract a sequence of n lines from a file 0ktalmagik Shell Programming and Scripting 4 06-30-2006 12:24 AM
appending string to text file based on search string malaymaru Shell Programming and Scripting 1 06-09-2006 09:53 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-21-2006
MrPeabody MrPeabody is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 3
Adding a sequence string to a file

I have a pipe delimited file I need to add a sequence number to in the third field. The record fields will be variable length, so I have to parse for the second pipe. Another requirement is that the sequence number must be unique to all records in the file and subsequent files created, so the sequence numbers will be maintained by an Oracle sequence.

I'm thinking basically select nextval into a variable and insert it, but I'm not that certain on the best approach to doing so with a ksh. Any ideas on how to insert this sequence in a ksh? I'm not very familiar with sed/awk.

TIA

before:

rec1field1sdss|rec1field2||rec1field4|rec1field5
rec2field1ff|rec2field2sds||rec2field4sdsds|rec2field5
rec3field1qwe|rec3field2wsxd||rec3field4sds|rec3field5

after:

rec1field1sdss|rec1field2|1|rec1field4|rec1field5
rec2field1ff|rec2field2sds|2|rec2field4sdsds|rec2field5
rec3field1qwe|rec3field2wsxd|3|rec3field4sds|rec3field5
  #2 (permalink)  
Old 07-21-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Do you only want to use ksh? Try doing it like this:

Code:
i=1
while read line; do
echo ${line%\|\|*}\|$i\|${line#*\|\|}
i=$(($i+1))
done < filename

Check the man page of ksh for explaination of the echo command.
  #3 (permalink)  
Old 07-21-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,433
Using awk :

Code:
awk -v FS='|' -v OFS='|' '{$3=NR ; print}' filename

-v FS='|' Set input field separator to |
-v OFS='-' Set Output field separator to |
'{$3=NR ; print}' Code executed for every input record
$3=NR Set field 3 equal to the Record number
print Print modified record to stdout


Jean-Pierre.
  #4 (permalink)  
Old 07-21-2006
MrPeabody MrPeabody is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 3
maintaining the sequence number

Thanks for the help. I would love to use the ksh only method, but I have to maintain the sequence number across multiple files.

ex:

file1:

field1|field2|1|field3...
field1|field2|2|field3...


file2, which will be generated on the following day:

field1|field2|3|field3...
field1|field2|4|field3...

These sequence numbers must remain unique to the records across files so we can keep track of them. They will be sent to a vendor and they will send them back at a later time. The boss wants to use an Oracle sequence to maintain uniqueness across all records sent out. Any ideas?


TIA
  #5 (permalink)  
Old 07-21-2006
Dhruva's Avatar
Dhruva Dhruva is offline
Registered User
  
 

Join Date: Mar 2006
Location: India
Posts: 255
If you must use oracle sequence to put sequence number in file then You may need to use UTL_FILE package of oracle.
Other method is create some temp table in oracle load these files into that table with sql loader utility with third field as oracle sequence.Now select all records from that table concatenated with | and spool that file.
  #6 (permalink)  
Old 07-21-2006
MrPeabody MrPeabody is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 3
Utl_file

Not enough time to read up on UTL_FILE, I will most likely have to use the second option.

Thanks for all your help, you have saved me a bunch of time spinning my wheels...
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 11:44 AM.


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