Perl: substitute multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: substitute multiple lines
# 1  
Old 11-02-2010
Perl: substitute multiple lines

I'd like to do this using Perl command line (sorry no awk, sed, etc.)

replace the 3 middle lines in a file by one line:


Code:
aaa 123
bbb 234
ccc 34567
dd 4567
ee 567

to:
Code:
aaa 123
AAA
ee 567

I tried this but not working:

Code:
perl -pi -e "s/aaa\ 123\nbbb\ 234\nccc\ 34567/AAA/" file

any ideas?

Last edited by radoulov; 11-03-2010 at 03:54 PM.. Reason: Code tags, please!
# 2  
Old 11-02-2010
Quote:
Originally Posted by tintin78899
I'd like to do this using Perl command line (sorry no awk, sed, etc.)

replace the 3 middle lines in a file by one line:


aaa 123
bbb 234
ccc 34567
dd 4567
ee 567

to:
aaa 123
AAA
ee 567

I tried this but not working:

perl -pi -e "s/aaa\ 123\nbbb\ 234\nccc\ 34567/AAA/" file

any ideas?
You'll need to enable slurp mode in order to work with multiple lines -

Code:
$
$
$ cat f2
aaa 123
bbb 234
ccc 34567
dd 4567
ee 567
$
$
$ perl -pne 'BEGIN {undef $/} s/bbb 234\nccc 34567\ndd 4567/AAA/' f2
aaa 123
AAA
ee 567
$
$
$

tyler_durden
# 3  
Old 11-03-2010
Bug

Thanks durden_tyler. that worked !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script: matching multiple lines error

Dear Perl users, Could somebody help me how to fix my code so I can get my desired output. Here is the data: Pattern Gabriel halo1 halo2 end Pattern Andreas halo1 halo2 endI want to grep multiple lines between the pattern /Pattern Gabriel / and /end/. Then I will store the output into... (6 Replies)
Discussion started by: askari
6 Replies

2. Shell Programming and Scripting

Perl Regex matching multiple lines

I need a way to extract data from X 4T Solution 21 OCT 2011 37 .00 to account 12345678 User1 user2 X 4T Solution Solution Unlimited 11 Sep 2009 248 .00 to account 87654321 user3 user4 I need it to extract 'X' '37.00' and account number 12345678. I have extracted above stuff... (3 Replies)
Discussion started by: chakrapani
3 Replies

3. Shell Programming and Scripting

Multiple lines into one using PERL or SHELL

Hi All, I need your help to solve problem using either PERL script or SHELL script. We are receving a file, in which one record is coming in multiple rows. The main problem is, we are not able to differenciate when the 1st record ends and where the second record starts. For example, ... (4 Replies)
Discussion started by: Amit.Sagpariya
4 Replies

4. Shell Programming and Scripting

Using Perl to Merge Multiple Lines in a File

I've hunted and hunted but nothing seems to apply to what I need. Any help will be much appreciated! My input file looks like (Unix): marker,allele1,allele2 RS1002244,1,1 RS1002244,1,3 RS1002244,3,3 RS1003719,2,2 RS1003719,2,4 RS1003719,4,4 Most markers are listed 3 times but a few... (2 Replies)
Discussion started by: Peggy White
2 Replies

5. Shell Programming and Scripting

PERL: removing blank lines from multiple files

Hi Guru's , I have a whole bunch of files in /var/tmp that i need to strip any blank lines from, so ive written the following script to identify the lines (which works perfectly).. but i wanted to know, how can I actually strip the identified lines from the actual source files ?? my... (11 Replies)
Discussion started by: hcclnoodles
11 Replies

6. Shell Programming and Scripting

Substitute specific lines with lines from another file

Hello All, I am new to this forum. I am currently facing a problem in manipulating files. I have two files called old-matter and new-matter # cat old-matter abc: this, is a, sample, entry byi: white board, is white in color rtz: black, board is black qty: i tried, a lot asd: no... (1 Reply)
Discussion started by: rahmathulla
1 Replies

7. Shell Programming and Scripting

[Help] PERL Script - grep multiple lines

Hi Gurus, I need some help with the "grep" command or whatever command that you think suitable for me. I'm about to write a perl script to extract a report from the system and submit it to the end users. The input for the script will consist of 3 element. 1) Generation ID 2) Month 3) Year... (6 Replies)
Discussion started by: miskin
6 Replies

8. Shell Programming and Scripting

PERL: add string to multiple lines

Dear all, I am stuck while trying to add a string to multiple lines. Let me try to explain using an example: Input: -------- myExample_send ("MasterSends", n, "Data Type", MPI_INT); correct Output:... (4 Replies)
Discussion started by: bonny
4 Replies

9. Shell Programming and Scripting

read and match multiple lines in perl

Could any one tell me how to read and match multiple lines in perl? Did this code below still work in this situation? while (<FILE>) { if (/ /) { } } Thanks a lot! (5 Replies)
Discussion started by: zx1106
5 Replies

10. Shell Programming and Scripting

Adding 3 Lines to Multiple c and h files with perl

Hello, i need some help with a perl script. i need to add the lines: #ifdef LOGALLOC #include "logalloc.h" #endif // LOGALLOC To all c and h files in a project with subdirectories. Logalloc is a tool to log all *alloc and free's in a text file, it defines the *alloc funtions new.... (2 Replies)
Discussion started by: Lazzar
2 Replies
Login or Register to Ask a Question