Multi-line Substitution Gone Awry - perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multi-line Substitution Gone Awry - perl
# 1  
Old 05-04-2008
Multi-line Substitution Gone Awry - sed

I've been working on this all night and finally have to ask for help... and not just from my coffee pot.

I need to replace a line of text only when it is proceeded by a line containing only the letter "H"

Input:
Code:
H      -2.204711   -0.922090   -0.024814

P
6-311+G(d)
****
C
6-311+G(d)
****
H
6-311+G(d)
****

Desired Output:
Code:
H      -2.204711   -0.922090   -0.024814

P
6-311+G(d)
****
C
6-311+G(d)
****
H
6-311++G(d,p)
****

I've tried many variations of perl and sed code that I have found here and elsewhere. As a result I've managed to do the opposite of what I wanted or to make a global substitution in dozens of different ways, but never have I managed the desired substitution.

My most recent attempts have been of this general nature:
Code:
perl -pe '$i=1 if (m+H+);if(m+FIND+) {if($i=1){s/FIND/REPLACE/} else {$i=0}}' input > output


I'm open to any options and am only using perl because it's what I'm most familiar with.

While the find and replace expressions I've been using for the above example may be incorrect I know that there is also an issue with the substitution code because I have used it on a simplified example containing only friendly characters and have encountered the same issues.

Last edited by EmperorNorton; 05-04-2008 at 12:48 PM.. Reason: no longer a perl question
# 2  
Old 05-04-2008
sed can do the job.
Code:
sed 'N;/^H/s/(d)/(d,p)/g' input > output

# 3  
Old 05-04-2008
Thank you so much!

Me thinks it's time to sit down and learn some sed and awk.
# 4  
Old 05-04-2008
Data

Something not working at all is one thing, but something inexplicably working sometimes is an even great frustration.

The code posted by danmero works great for the code example I had given, but performs no substitution when given the following as input:
Code:
H	 3.228216   -1.558532	-0.403837

P
6-311+G(d)
****
O N
6-311+G(d)
****
C
6-311+G(d)
****
H
6-311+G(d)
****

I use:
Code:
sed 'N;/^H/s/+G(d)/++G(d,p)/g' in > out

and no change is made in the output. Anyone have any ideas? I'm really confused because it seems to me that the conditions for the substitution don't really change at all, and the condition appears in this input file exactly as it does in the input which work with the sed code.
# 5  
Old 05-04-2008
Code:
perl  -00 -pe's/(\n?H\n.*?)(\Q+G(d)\E)/$1++G(d,p)/g' file


With awk (nawk or /usr/xpg4/bin/awk on Solaris):

Code:
awk 'f{sub(/\+G\(d\)/,"++G(d,p)");f=0}1;/^H$/{f++}' file

or sed:

Code:
sed '/^H$/{n;s/+G(d)/++G(d,p)/;}' file


Last edited by radoulov; 05-05-2008 at 06:30 AM.. Reason: corrected
# 6  
Old 05-04-2008
Quote:
Originally Posted by EmperorNorton
Code:
H	 3.228216   -1.558532	-0.403837

P
6-311+G(d)
****
O N
6-311+G(d)
****
C
6-311+G(d)
****
H
6-311+G(d)
****

Strange, my solution work for your second example. Next time try to provide some informations about your work environment.
Quote:
Originally Posted by EmperorNorton
I use:
Code:
sed 'N;/^H/s/+G(d)/++G(d,p)/g' in > out

and no change is made in the output. Anyone have any ideas? I'm really confused because it seems to me that the conditions for the substitution don't really change at all, and the condition appears in this input file exactly as it does in the input which work with the sed code.
Why you need a second plus ?
# 7  
Old 05-04-2008
Quote:
Originally Posted by radoulov
Code:
perl  -00 -pe's/(\nH\n.*?)(\Q+G(d)\E)/$1++G(d,p)/g' file


With awk (nawk or /usr/xpg4/bin/awk on Solaris):

Code:
awk 'f{sub(/\+G\(d\)/,"++G(d,p)");f=0}1;/^H$/{f++}' file

or sed:

Code:
sed '/^H$/{n;s/+G(d)/++G(d,p)/;}' file


The sed code has worked properly on all the inputs I've tried so far.
Thank you very much for your time.

Now if only I could figure out what the critical difference is...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multi line log files to single line format

I want to read the log file which was generate from other command . And the output was having multi line in log files for job name and server name. But i need to make all the logs on one line Source file 07/15/2018 17:02:00 TRANSLOG_1700 Server0005_SQL ... (2 Replies)
Discussion started by: ranjancom2000
2 Replies

2. Shell Programming and Scripting

Help with reformat single-line multi-fasta into multi-line multi-fasta

Input File: >Seq1 ASDADAFASFASFADGSDGFSDFSDFSDFSDFSDFSDFSDFSDFSDFSDFSD >Seq2 SDASDAQEQWEQeqAdfaasd >Seq3 ASDSALGHIUDFJANCAGPATHLACJHPAUTYNJKG ...... Desired Output File >Seq1 ASDADAFASF ASFADGSDGF SDFSDFSDFS DFSDFSDFSD FSDFSDFSDF SD >Seq2 (4 Replies)
Discussion started by: patrick87
4 Replies

3. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

4. Shell Programming and Scripting

Perl one-line substitution syntax

Hi, With the following Perl syntax, how to print the $_ value after the substitution? s/(\s*|\n)//g foreach (<>); If I use the below code, it produces some numeric output print s/(\s*|\n)//g foreach (<>); (2 Replies)
Discussion started by: royalibrahim
2 Replies

5. Shell Programming and Scripting

Multi-line filtering based on multi-line pattern in a file

I have a file with data records separated by multiple equals signs, as below. ========== RECORD 1 ========== RECORD 2 DATA LINE ========== RECORD 3 ========== RECORD 4 DATA LINE ========== RECORD 5 DATA LINE ========== I need to filter out all data from this file where the... (2 Replies)
Discussion started by: Finja
2 Replies

6. Shell Programming and Scripting

Joining multi-line output to a single line in a group

Hi, My Oracle query is returing below o/p ---------------------------------------------------------- Ins trnas value a lkp1 x a lkp1 y b lkp1 a b lkp2 x b lkp2 y ... (7 Replies)
Discussion started by: gvk25
7 Replies

7. Shell Programming and Scripting

Merge multi-line output into a single line

Hello I did do a search and the past threads doesn't really solve my issue. (using various awk commands) I need to combine the output from java -version into 1 line, but I am having difficulties. When you exec java -version, you get: java version "1.5.0_06" Java(TM) 2 Runtime... (5 Replies)
Discussion started by: flagman5
5 Replies

8. Shell Programming and Scripting

perl regex multi line cut

hello mighty all there's a file with lots of comments.. some of them looks like: =comment blabla blablabla bla =cut i'm trying to cut this out completely with this code: $line=~s/^=.+?=cut//sg; but no luck also tryed to change it abit but still I don't understand how the... (9 Replies)
Discussion started by: tip78
9 Replies

9. Shell Programming and Scripting

How to use Perl to join multi-line into single line

Hello, Did anyone know how to write a perl script to merge the multi-line into a single line where each line with start at timestamp Input--> timestamp=2009-11-10-04.55.20.829347; a; b; c; timestamp=2009-11-10-04.55.20.829347; aa; bb; cc; (5 Replies)
Discussion started by: happyday
5 Replies

10. Shell Programming and Scripting

How to use Perl to merge multi-line into single line

Hi, Can anyone know how to use perl to merge the following multi-line information which beginning with "BAM" into one line. For each line need to delete the return and add a space. Please see the red color line. ******Org. Multi-line) BAM admin 101.203.57.22 ... (3 Replies)
Discussion started by: happyday
3 Replies
Login or Register to Ask a Question