Insert text between delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert text between delimiter
# 1  
Old 03-06-2008
Insert text between delimiter

Can someone help me on this?

I'm creating an Insert stmt script but Oracle does not accept blanks values. How can I insert the word null between two commas? I'm guessing awk or sed.

Is there a good post or site with easy to understand info on awk and sed? I'm really new to unix scripts Smilie

Thanks.
# 2  
Old 03-06-2008
Quote:
Originally Posted by ystee
Can someone help me on this?

I'm creating an Insert stmt script but Oracle does not accept blanks values. How can I insert the word null between two commas? I'm guessing awk or sed.

Is there a good post or site with easy to understand info on awk and sed? I'm really new to unix scripts Smilie

Thanks.
Show how the input and what the output looks like.
# 3  
Old 03-06-2008
Input and output sample

Input: INSERT into caps.prodfile VALUES ('MK992','',,,,'','','');
Output: INSERT into caps.prodfile VALUES ('MK992','',NULL,NULL,NULL,'','','');

I've tried the following

sed 's/,,/,NULL,/g' prodfile.sql

but it only inserts the first NULL in each line.
And there are several hundred Insert statements in a file. Thanks.
# 4  
Old 03-06-2008
Quote:
Originally Posted by ystee
Input: INSERT into caps.prodfile VALUES ('MK992','',,,,'','','');
Output: INSERT into caps.prodfile VALUES ('MK992','',NULL,NULL,NULL,'','','');

I've tried the following

sed 's/,,/,NULL,/g' prodfile.sql

but it only inserts the first NULL in each line.
And there are several hundred Insert statements in a file. Thanks.
Code:
awk -F"," '{
   for (i = 1; i <= NF; ++i) {
      if ($i == "")
         printf("NULL,")
      else {
         if (i < NF)
            printf("%s,", $i)
         else
            printf("%s\n", $i)
      }
   }
}' file

# 5  
Old 03-06-2008
Java

There's sure to be a way to do this in sed, but I'm pretty poor with sed so here's some (really ugly) perl:

Code:
#!/usr/bin/perl -w
while (<>) {
  while ($_ =~ s/,,/,NULL,/) { print "" }
  print $_;
}

# 6  
Old 03-06-2008
Thanks

The code works perfectly Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a value between two empty delimiter in the file.

Would like to insert value between two empty delimiter and at the very last too if empty. $ cat customerleft.tbl 300|Customer#000000300|I0fJfo60DRqQ|7|17-165-193-5964|8084.92|\N|p fluffily among the slyly express grouches. furiously express instruct||||||||||||||||||||||||\N... (3 Replies)
Discussion started by: Mannu2525
3 Replies

2. Shell Programming and Scripting

Linux shell script to insert new lines based on delimiter count

The input file is a .dat file which is delimited by null (^@ in Linux). On a windows PC it looks something like this (numbers are masked with 1). https://i.imgur.com/nta2Gqp.jpg The entire file is in one row but it has multiple records - each record contains 80 fields i.e. there are 81 counts... (9 Replies)
Discussion started by: digitalnirvana
9 Replies

3. Shell Programming and Scripting

Insert a new column with sequence number (Delimiter as comma)

Hi All, I have a file which has data like a,b c,d e,f g,h And I need to insert a new column at the begining with sequence no( 1 to n) 1,a,b 2,c,d 3,e,f 4,g,h Please let me know how to acheive this in unix (3 Replies)
Discussion started by: weknowd
3 Replies

4. Shell Programming and Scripting

[Solved] Insert tabs as delimiter

Hello all, I have an unstructured file with space as delimiter , which I want to structure. The output file should actually have only 5 columns with tab as delimiter. The 4th column can have only 3 values ( biological_process , cellular_component , molecular_function ) Here is how the... (12 Replies)
Discussion started by: ritakadm
12 Replies

5. Shell Programming and Scripting

Shell script to put delimiter for a no delimiter variable length text file

Hi, I have a No Delimiter variable length text file with following schema - Column Name Data length Firstname 5 Lastname 5 age 3 phoneno1 10 phoneno2 10 phoneno3 10 sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies

6. Shell Programming and Scripting

Selecting Specific Columns and Insert the delimiter TAB

Hi, I am writing a Perl Script for the below : I have a data file that consists of the header information which is 231 Lines and the footer information as 4 lines. The total number of line including the header and footer 1.2 Million with Pipe Delimited file. For example: Header Information:... (4 Replies)
Discussion started by: filter
4 Replies

7. Shell Programming and Scripting

insert delimiter

I have a data file that I would like to add delimiters to. Example: Turn This: 20110624000744000693000704000764 Into This: 20110624,000744,000693,000704,000764 I found this link but the only issue is I do not know how many colums I will have. The first field would needs to be 8... (9 Replies)
Discussion started by: oldman2
9 Replies

8. UNIX for Advanced & Expert Users

Insert Delimiter at fixed locations in a flat file

Hi Can somebody help me with solution for this PLEASE? I have a flat file and need to insert delimiters at fixed positions in all the lines so that I can easily convert into EXCEL with columns defined as per their width. For Example Here is the file { kkjhdhal sdfewss sdtereetyw... (7 Replies)
Discussion started by: jd_mca
7 Replies

9. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

10. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies
Login or Register to Ask a Question