Adding 3 Lines to Multiple c and h files with perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding 3 Lines to Multiple c and h files with perl
# 1  
Old 10-27-2003
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.
#define malloc(size) logmalloc(size,__FILE__,__LINE__)


anyone fluent in perl could give me some hints?

thanks in advance
Lazzar
# 2  
Old 10-28-2003
Almost!

well i have it almost but arrgh

1. with perl
perl -pi -e "s:^:#ifdef MALLOC_TRACE\n#include <logalloc.h>\n#endif\n:" *.test

almost right but it replaces every begining of a line(^) with my string, it only needs to replace the 1st line!


2. with sed
sed -e "1s:^:#ifdef MALLOC_TRACE\n#include <logalloc.h>\n#endif\n:" *.test

sed replaces only the first line (notice the 1 infront of the 's'. (the perl script dosent support the 1)
But sed dosent recognize the newlines, i dont know how to define them in the cshell.

2 questions:
----------------
1.how do i declare a \n (newline) in the cshell?

2. how can i limit my perl script to only one (the first) line?

Lazzar
# 3  
Old 10-28-2003
ok got sed working but i'd love to do it with perl, cause i think it would be alot faster.

sed -f script *.c

script:
1s:^:#ifdef MALLOC_TRACE\
#include <logalloc.h>\
#endif\
:

it replaces the beginning of the first line of all *.c files with my 3 ifdef lines.

i still need help with the perl script tho

Lazzar
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

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

3. Shell Programming and Scripting

adding a column of unique string to multiple files

Hi, I need to add a column of unique string to individual csv file. my individual csv file looks like this and each file has variable row length: 178.52,39.4,train,0.003355544375334351,39.15752753933254,0.4895933968953914... (7 Replies)
Discussion started by: ida1215
7 Replies

4. Shell Programming and Scripting

Adding Multiple Files via Columns

I have a number of files with multiple rows that I need to add together. Let say I have 10 files: Each file has a great number of rows and columns. I need to add these files together the following way. In other words, If, for example, file A occupies Columns 1 to 19, I want to add file B... (7 Replies)
Discussion started by: Ernst
7 Replies

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

6. Shell Programming and Scripting

Adding lines to files based on file extension

I have posted this before but did not get many replies, so here it goes again. I have several files name like this If the file extension is 1a, I woould like to add at the beggining of the file the following sequence If the file extension is 1b, thn the entry that should be added is the next... (2 Replies)
Discussion started by: Xterra
2 Replies

7. Shell Programming and Scripting

Adding lines to files based on their names

I have several files on a folder something like this: If the file starts with 1a, I would like to add the following at the very beggining of the file If the file starts with 1b then I should add the following For files starting with 6 So on and so forth. Thus, at the end each file will... (18 Replies)
Discussion started by: Xterra
18 Replies

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

9. Shell Programming and Scripting

Adding Multiple Lines to Multiple Files

Hello, I have three lines I need to add to hundreds of files. The files are all in the same format and contain the same information. I want to add the same information to the files. The new lines of information are similar. Here is an example of the three lines: line1: line2 =... (2 Replies)
Discussion started by: dayinthelife
2 Replies

10. Shell Programming and Scripting

Adding columns to excel files using Perl

How do I add 4 columns to an excel file using Perl? The 4 headers for those columns will all have different names? Please help and I greatly appreciate... (1 Reply)
Discussion started by: dolo21taf
1 Replies
Login or Register to Ask a Question