PERL: add string to multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL: add string to multiple lines
# 1  
Old 05-18-2008
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:
-----------------
#pragma my my_Example_send ("MasterSends",
#pragma my n,
#pragma my "Data Type",
#pragma my MPI_INT);


I search for "myExample_" and want to replace it with "#pragma my myExample_". My perl script works fine if the function call doesn't exceed one line. See below a section of the perl script I use:
******************************************
# Original string
$original = "myExample_";
# New string
$replacement = "#pragma my myExample_";
undef $/;

# Open each file otherwise print error message
foreach $file (@ARGV)
{
if ( ! open(INPUT,"<$file") )
{
print STDERR "Can't open file $file\n";
next;
}

# Read input file as one long record
$data=<INPUT>;
close INPUT;


# replace original string with new string
if ( $data =~ s/$original/$replacement/gi )
{
....
}
}
******************************************

However, if the function call exceeds one line, then obviously I get something similar to:
Current Output:
------------------
#pragma my myExample_send ("MasterSends",
n,
"Data Type",
MPI_INT);

Which would result in an error while compilation.

Can anybody "coach" me how to add "#pragma my " to all lines belonging to the myExample_send function call?

Thanks a lot in advance!
# 2  
Old 05-19-2008
Hi,
You are reading only one line with $data.
Use @data instead. (This will read the entire file )

Quote:
Originally Posted by bonny

# Read input file as one long record
$data=<INPUT>;
# 3  
Old 05-19-2008
Thanks for the reply!
However, "@data" doesn't fix the problem I described above, does it?
# 4  
Old 05-19-2008
@data=<INPUT>;
.
.

# replace original string with new string
foreach $data(@data) {
if ( $data =~ s/$original/$replacement/gi )
{
....
}
}


Quote:
Originally Posted by bonny
Thanks for the reply!
However, "@data" doesn't fix the problem I described above, does it?
# 5  
Old 05-19-2008
That's exactly what I've tried!
Nevertheless, the output is still:
------------------------------
#pragma my myExample_send ("MasterSends",
n,
"Data Type",
MPI_INT);
------------------------------

instead of:
------------------------------
#pragma my my_Example_send ("MasterSends",
#pragma my n,
#pragma my "Data Type",
#pragma my MPI_INT);
------------------------------

I think "undef $/;" already ensures that I read the entire file instead of reading line by line.

However, regarding the output I am looking for:
My thinking is that I also have to look for ";" and keep adding "#pragma my " until ";" appears.
But then I probably *have to* do it line by line, right?

Any thoughts out there?
Please let me know!
Thanks a lot!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a string with multiple lines

Hello Guys, I need to replace a string with multiple lines. For eg:- ABC,DEF,GHI,JKL,MNO,PQR,STU need to convert the above as below:- ABC,DEF, GHI1 GHI2 GHI3, JKL,MNO, PQR1 PQR2 PQR3, STU i have tried using code as:- (2 Replies)
Discussion started by: jassi10781
2 Replies

2. Shell Programming and Scripting

Remove multiple lines from a particular string to particular string

Hi, I have a file containing the DDLs of tables in a schema. From that I need to remove all the lines from a starting string till a specific string. Here is an example. File1.txt ------------- CREATE TABLE "SCHEMA1"."LKP11_TBL_USERS" ( "ID" NUMBER(8,0) NOT NULL ENABLE, "USER_ID"... (3 Replies)
Discussion started by: satyaatcgi
3 Replies

3. Shell Programming and Scripting

TCL expect out string with multiple lines

Hello, I will be sending this command to a specific COMID: exp_send-i $COMID "fdisk -l | grep Disk | awk '{print $2}'" The command will produce this output: /dev/sda /dev/sdb etc.. the problem is how do I store the output in a variable in TCL, I am currently using this to grab the... (1 Reply)
Discussion started by: h0ujun
1 Replies

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

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

6. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

7. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

8. Shell Programming and Scripting

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: 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 (2 Replies)
Discussion started by: tintin78899
2 Replies

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

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