Visit Our UNIX and Linux User Community


Search a character and replace it with multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search a character and replace it with multiple lines
# 1  
Old 09-22-2014
Search a character and replace it with multiple lines

This is for AIX 6.1, I've a flat file and the format is like this

Code:
DECLARE
    some statements;

BEGIN

    some statements;

END;

I've to search BEGIN and replace it with the following 4 lines
Code:
BEGIN
For x in 1..1
LOOP
BEGIN

Similarly I've to search END and replace it with the following text in 6 lines. I cannot combine lines and I need to put semi colon at the end as mentioned below in certain lines.

Code:
EXCEPTION 
	when DUP_VAL_ON_INDEX then
		continue;
END;
END LOOP;
END;

Can you please help?

Thanks

Mukul

Last edited by Corona688; 09-22-2014 at 04:53 PM..
# 2  
Old 09-22-2014
Hello Mukul Sharma,

Welcome to forum, please use code tags while using putting commands and codes in post as per forum rules. Now for your post, could you please let us know the input and expected output, so that we may help you in same. Kindly make sure you are using code tags while putting commands and codes in post.


Thanks,
R. Singh
# 3  
Old 09-22-2014
Nothing AIX specific, moving.
# 4  
Old 09-22-2014
This is the output from shell script

sharmam@cysvigdcdbora10:/home/sharmam>./repl.ksh
$BEGIN
$END;

This is the flat file to be converted.

sharmam@cysvigdcdbora10:/home/sharmam>cat proc.txt
Code:
DECLARE
    null_value CHAR(1) := NULL;
    statement1 CHAR(63);
    statement2 CHAR(56);

BEGIN
    statement1 := 'INSERT INTO rateheader VALUES (:0, :1, :2, :3, :4, :5, :6)';
END;
/

This is the shell script

sharmam@cysvigdcdbora10:/home/sharmam>cat repl.ksh

Code:
BEGIN="
BEGIN\
FOR X in 1..1\
LOOP\
        BEGIN\
"

END="
EXCEPTION
        WHEN OTHERS THEN
                CONTINUE;
END;
END LOOP;
"
sed -n 's/BEGIN/$BEGIN/p' proc.txt
sed -n 's/END/$END/p' proc.txt

The expected output is

DECLARE
null_value CHAR(1) := NULL;
statement1 CHAR(63);
statement2 CHAR(56);
BEGIN
FOR X in 1..1
LOOP
BEGIN
statement1 := 'INSERT INTO rateheader VALUES (:0, :1, :2, :3, :4, :5, :6)';
END="
EXCEPTION
WHEN OTHERS THEN
CONTINUE;
END;
END LOOP;
/

Can you please suggest?

Thanks
# 5  
Old 09-22-2014
Following is the fixed code, based on your own approach with sed, which did not work, I guess.
(I cannot guarantee it will work with AIX' version of sed, but it's worth to try, let me know)
Code:
BEGIN="\\
BEGIN\\
FOR X in 1..1\\
LOOP\\
        BEGIN\\
"

END="\\
EXCEPTION\\
        WHEN OTHERS THEN\\
                CONTINUE;\\
END;\\
END LOOP;\\
"
sed "s/BEGIN/$BEGIN/;s/END/$END/" proc.txt

---------- Post updated at 10:44 PM ---------- Previous update was at 10:40 PM ----------

Following is another approach, based on awk (note how no wild escaping is required):
Code:
$ cat ins1
BEGIN
For x in 1..1
LOOP
BEGIN
$ 
$ cat ins2
EXCEPTION 
    when DUP_VAL_ON_INDEX then
        continue;
END;
END LOOP;
END;
$

Code:
awk -v i1="`<ins1`" -v i2="`<ins2`" '
    !/BEGIN/ && !/END/ {print}
    /BEGIN/ {print i1}
    /END/ {print i2}
' file-to-modify

# 6  
Old 09-23-2014
Thanks all, it worked. I really appreciate your help.

Mukul

---------- Post updated at 11:36 AM ---------- Previous update was at 10:00 AM ----------

Hi All, The above sed command "sed "s/BEGIN/$BEGIN/;s/END/$END/" proc.txt" is throwing a semi colon at the next line after the "END;"

END;
END LOOP;
END;
; <<<<<---- This need to be removed and the / should move up.
/

Can you please help?

Thanks
# 7  
Old 09-23-2014
This will remove the semicolon:
Code:
sed "s/BEGIN/$BEGIN/;s/END;/$END/" proc.txt

If you want the slash "/" to move up, then replace this piece
Code:
END LOOP;\\
"

with
Code:
END LOOP;"


Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replace character in odd or even lines

Hello, I'm here again asking for your precious help. I'm writing some code to convert csv files to html. I want to highlight header and also I want to have rows with alternate colors. So far this is my work###Let's format first line only with some color cat $fileIN".tmp1" | sed '1... (7 Replies)
Discussion started by: emare
7 Replies

2. UNIX for Dummies Questions & Answers

Search for a particular word and replace the first character

Hi Unix gurus, I've a dna sequence in a file format known as fasta format (sequence header starts with > and ignored), an example shown below: >sequence_1 CGTATTCTCCGAATACC ATACG >sequence_2 CAGATTTTCAAATACCCCC In a file like this I want to do the following three search and replace. The... (4 Replies)
Discussion started by: Fahmida
4 Replies

3. Shell Programming and Scripting

Regex:search/replace but not for escaped character

Hi Input: - -- --- ---- aa-bb-cc aa--bb--cc aa---bb---cc aa----bb----cc Output: . - -. -- aa.bb.cc (7 Replies)
Discussion started by: chitech
7 Replies

4. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

5. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

6. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

7. Shell Programming and Scripting

Replace multiple occurances of same character with a single character.

Hi all, Greetings, I have the following scenario, The contents of main file are like : Unix|||||forum|||||||||||||||is||||||the||best so||||||be|||||on||||||||||||||||||||||||||||||||||||||||||||it And i need the output in the following form: Unix=forum=is=the=best so=be=on=it ... (3 Replies)
Discussion started by: dipanchandra
3 Replies

8. UNIX for Dummies Questions & Answers

vi Search for text, Replace with <CR> or control character.

Greetings, Using vi, how can I change the following text: -I/myviews/nexus_7400rel/vobs/nexus/platforms/97400/include -I/myviews/nexus_7400rel/vobs/nexus/modules/i2c/7400/include -I/myviews/nexus_7400rel/vobs/nexus/modules/surface/7400/include Into this:... (4 Replies)
Discussion started by: omega949
4 Replies

9. Shell Programming and Scripting

Need to search and replace based on character count

Hi, I wanted to add a newline character after every 100 characters in a file using a awk or shell without reading each line of the file. I want to run a command on the complete file. This does based on a string but i want to add a new line after every 100 characters ir-respective of the... (3 Replies)
Discussion started by: vijaykrc
3 Replies

10. Shell Programming and Scripting

Replace a perticular character of all lines of a file

Hi all, I am new to UNIX, so sorry if my question seem stupid to u. well i want to replace the first character of first 30 lines of a file, only if the first character is h. and in anothe script i want to replace a particular string/character say hello/h of a file.Condition: It should... (1 Reply)
Discussion started by: abovais
1 Replies

Featured Tech Videos