Sponsored Content
Full Discussion: UNIX multiline replace
Top Forums UNIX for Dummies Questions & Answers UNIX multiline replace Post 302909050 by bakunin on Monday 14th of July 2014 05:57:04 AM
Old 07-14-2014
Quote:
Originally Posted by sheetal.arun
OS is AIX Unix v1.6
Somehow i doubt that. If you want to find the version information do a oslevel -s. Fortunately, your question is not related to the OS version at all, see below.

Quote:
Originally Posted by sheetal.arun
We have a database export file which needs to be formatted as below

Code:
Create view ABC1 as Locking ABC1 
for Access select * from PQR

Create view ABC2 as Locking
ABC2 for access
select * from PQR

Create view ABC3
as Locking ABC3
for
Access 
select * from PQR


Output
Code:
Create view ABC1 as Locking row for access select * from PQR

Create view ABC2 as Locking row for access
select * from PQR

Create view ABC3 as Locking row for access
select * from PQR

To solve such problems it usually helps to rephrase the requirements.

As far as i can see, you want to concatenate every line following an empty line until you hit a blank line. Then print everything in one line, up to the "select"-statement and in a second separate line the statement itself. Here we go. The following is a commented version, remove any comment and the trailing blanks in each line before running it:

Code:
:start                            ; branching target "start"
/^$/ b print                      ; if we find an empty line, go to "print"
$    b print                      ; if it is the last line, go to "print"
H                                 ; otherwise: append line to hold space
d                                 ;       and delete it from pattern space
b start                           ;       then goto "start" (start over)
:print                            ; start of "print" subroutine
    {
      g                           ; copy hold space to pattern space
      s/\n/ /g                    ; remove all newline characters
      s/select.*//                ; the remove "select" and everything after
      s/^ *//                     ; remove eventually leading space
      s/  */ /g                   ; replace multiple spaces by a single one
      s/Locking/& row/            ; "Locking" -> "Locking row"
      s/Access/access/            ; "Access" -> "access"
      p                           ; finally print it
      g                           ; copy the hold space again
      s/.*select/select/p         ; remove everything before "select", print
      d                           ; delete pattern space
      h                           ; and copy it to hold space, thus clearing it
    }

save that to a file and issue:

Code:
sed -f /path/to/scriptfile /path/to/input > /path/to/output

If you want to include additional changes to your text (inserting words, lowercasing, uppercasing, ... add the respective rule between these two lines:

Code:
      s/Access/access/            ; "Access" -> "access"
      p                           ; finally print it

I hope this helps.

bakunin

Last edited by bakunin; 07-14-2014 at 07:32 AM..
This User Gave Thanks to bakunin For This Post:
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Multiline Grep

How does one do a search for a multiline regular experssion and output the results to a file. I know this won't work since grep only searches single lines: egrep '<a>.*?</a>' source.xml > output.xml Here are some sample patterns I'd like to match and output to a single file: ... (4 Replies)
Discussion started by: tolmark
4 Replies

2. Shell Programming and Scripting

Multiline replace problem

I have a data of the form 0.0117843924 0. 0. 0. 0. 0.011036017 0. 0. 0. 0. 0.0103351669 0. 0. 0. 0. 4839.41211 0. 0. 0. 0. 4532.08203 0. 0. 0. 0. I would like to insert a couple of blank lines before the 4839 line, every time it appears. The numbers in the... (2 Replies)
Discussion started by: mathis
2 Replies

3. Shell Programming and Scripting

grep command to replace multiline text from httpd.conf file on Fedora

Hi, I am a newbie to shell scripting and to Linux environment as well. In my project I am trying to search for following text from the httpd.conf file <Directory '/somedir/someinnerdir'> AllowOverride All </Directory> and then remove this text and again rewrite the same text. The... (1 Reply)
Discussion started by: bhushan
1 Replies

4. Shell Programming and Scripting

perl : replace multiline text between two marker points

Hi there I just wondered if someone could give me some perl advice I have a bunch of text files used for a wiki that have common headings such as ---++ Title blah ---++ Summary blah ---++ Details Here is the multiline block of text I wish to (6 Replies)
Discussion started by: rethink
6 Replies

5. Shell Programming and Scripting

How to replace word with multiline text using shell scripting.

Hi all I have data files which contain data as shown below: Line 5: FIDE INST_DESC: DIAM Co Ltd/Japan => MAID Co Ltd/Japan INST_NME: DIAM Co Ltd/Japan => MAID Co Ltd/Japan Line 6: FIDE INST_DESC: DIAM DL/Pimco US Bond Open Born in the USA => MAID DL/Pimco US Bond Open Born in the... (6 Replies)
Discussion started by: Ganesh_more
6 Replies

6. UNIX for Dummies Questions & Answers

Need Multiline sed help!!

Hey everyone, I'm new to sed and I need to create a script for inserting one line of code at the beginning of every method in a Xcode project (over 6,000 methods). Each method Structure is (+ or -) (Various declarations-- could span multiple lines) ({) I've tried for days, any guidance would be... (2 Replies)
Discussion started by: jimmyz
2 Replies

7. Shell Programming and Scripting

MultiLine Patterns

Experts, I am novice unix user. At my work, most of our DBA's work on creating DDL's to create new tables in production. At every week we need to validate the scripts (do peer review) and it takes a while and also it is not effective when we have like 150 tables created in the scripts. I am... (3 Replies)
Discussion started by: ysvsr1
3 Replies

8. Shell Programming and Scripting

Need to get multiline input

As per my requirement, I need to get a multiline input. It can be stored in a file, that's not a problem. User will be prompted to enter steps. he should able to enter the steps in multiple lines by pressing enter. All I know in read command that reads the input till we press enter. Can someone... (5 Replies)
Discussion started by: annamalaikasi
5 Replies

9. Shell Programming and Scripting

Bash script - How to update header of scripts in one pass - multiline search/replace

Hello. A find command return a list of file. For each fileReplace the content starting with the first "§" (of two) ending with last "ɸ" (of two), regardless of the content ( five lines ) by the following content (exactly) : §2019_08_23§ # # ... (8 Replies)
Discussion started by: jcdole
8 Replies
All times are GMT -4. The time now is 01:15 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy