UNIX multiline replace


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers UNIX multiline replace
# 1  
Old 07-14-2014
UNIX multiline replace

Hi
We have a database export file which needs to be formatted as below

Input
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



We tried using
Code:
sed -n '1h;1!H;${;g;s/Locking.*Access/Locking row for access/g;p;}'

but this takes first Locking and last Access and replaces everything in between

OS is AIX Unix v1.6


Please help!!

Last edited by sheetal.arun; 07-14-2014 at 05:08 AM.. Reason: Improve example
# 2  
Old 07-14-2014
Showing us a single line of input and a single line of output while talking about multiple occurrences of a pattern that may repeat on multiple lines leaves a lot to the imagination.

Do you just want to change ABC for A to row for a as shown in your example? Are there other strings that you want to change? Can these three word sequences appear multiple times on a single line? Can these three words be split across lines for a singe occurrence?

What operating system are you using? Are we restricted to a certain shell or set of tools to be used to solve this problem?

For what you have shown us, the much simpler command:
Code:
sed 's/Locking.*Access/Locking row for access/'

does everything you've shown us needs to be done.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 07-14-2014
Thanks Don ... Op has been updated
# 4  
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:
# 5  
Old 07-14-2014
Hi Bakunin

Requirement: Replace content between 'Locking' and 'Access' with 'Locking row for access'

The code in opening post tries to do that, but it ends up taking the first 'Locking' and last 'Access' and replace that as an entire pattern, due to use of .*

Since, there are multiple create view commands in the file (as seen in the input) and each needs to replace seperately

We have a workround where we pass each create command to a file and then run code on that individually. We were looking for a more effecient replace.
# 6  
Old 07-14-2014
Another option maybe, try:
Code:
awk '{$6="row"}1' RS= file

or

Code:
awk '{$6="row"}1' RS= ORS='\n\n' file

for double spaced output..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
Login or Register to Ask a Question