Adding a sequence string to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding a sequence string to a file
# 1  
Old 07-21-2006
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  
Old 07-21-2006
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  
Old 07-21-2006
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  
Old 07-21-2006
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  
Old 07-21-2006
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  
Old 07-21-2006
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...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help in adding sequence number to a file

Hi All , I have a file which contains data(comma separated) in below format : 500,Sourav ,kolkata ,8745775020,700091 505,ram,delhi ,9875645874,600032 510 ,madhu ,mumbai ,5698756430 ,500042 515 ,ramesh ,blore ,8769045601 ,400092 I want to add unique sequence number at the start of each... (7 Replies)
Discussion started by: STCET22
7 Replies

2. Shell Programming and Scripting

Get string of sequence from other file

Hi guys, Does anyone know how to get a string of sequence from other file? Should I use awk? Please see below. Thanks! LIST_FILE: >NAME1 >NAME3 >NAME5 >NAME7 >NAME8 SEQ_FILE: >NAME1 LEN75 100100101001010001010 >NAME2 LEN90 111010101010101101101 >NAME3 LEN27 101000101001010010101... (5 Replies)
Discussion started by: narachaid
5 Replies

3. Shell Programming and Scripting

[string processing]Adding new line in file

I have a report file which somewhere has a lines starting with word ".sub" I have a new variable named $new. What i am trying to do is insert the content of $new just before the occurrence of ".sub" in the report file. How can I do that? (11 Replies)
Discussion started by: animesharma
11 Replies

4. Shell Programming and Scripting

Going places with a Dolphin (adding to xml in sequence)

Hi all, I hope someone can help me write a script that: Reads the xml file which holds Dolphins "Places" in KDE. If a specific tag is not there, it should create a set for it, in order. so this little file lives in ~/.kde/share/apps/kfileplaces/bookmark.xml Here is my example. <?xml... (3 Replies)
Discussion started by: CtrlKey
3 Replies

5. UNIX for Dummies Questions & Answers

Adding a running sequence number while appending 2 files

Hi, I have to append 2 files and while appending i need to add a running sequence number (counter ) for each line at the first column. For e.g. If file x contains details as below. Tom Dick Harry Charlie and file y contains Boston Newyork LA Toledo Then the new file should... (1 Reply)
Discussion started by: kalyansid
1 Replies

6. UNIX for Dummies Questions & Answers

Adding Sequence Number to file

Hi All, I need to create a script which checks for a particular file for eg.kumar1.txt. If kumar1.txt is already exist the script should increment the file name as kumar2.txt and so on. Please Advise. Thanks & Regards, Kumar66 (2 Replies)
Discussion started by: kumar66
2 Replies

7. Shell Programming and Scripting

Adding sequence to the file

How do I add the sequence number to the file? I have a file seperated by commas. appusage,243,jsdgh,798 appusage,876,0989,900 . . appusage,82374,ajfgdh,9284 The output would be as below 1,appusage,243,jsdgh,798 2,appusage,876,0989,900 . . 100,appusage,876,0989,900 (5 Replies)
Discussion started by: smee
5 Replies

8. Shell Programming and Scripting

How to split a file with adding sequence number and extension.

I have a file name -HRCFTSIN05PLA1602100430444444 my requirement is to split the file in 10000 count each file and to add sequence number.rch at the end of each file. output should be in this format HRCFTSIN05PLA160210043044444401.rch HRCFTSIN05PLA160210043044444402.rch... (4 Replies)
Discussion started by: abhigrkist
4 Replies

9. Shell Programming and Scripting

Adding a sequence number within a file

Can someone please help. I need to add a sequence number to the start of each line of a file. It needs to be 6 characters long. ie 000001 next line starts 000002 etc. (4 Replies)
Discussion started by: Dolph
4 Replies

10. Shell Programming and Scripting

adding string to a file

I 've to add the two statements to some java files: setValid(false); // the call should be after the openeing curly brackets return true; // the "return true" before the closing curly brackets Input: boolean isValid() { . . . } Output: boolean isValid() { ... (8 Replies)
Discussion started by: andy2000
8 Replies
Login or Register to Ask a Question