Adding information to c file using perl


 
Thread Tools Search this Thread
Top Forums Programming Adding information to c file using perl
# 1  
Old 10-05-2011
Adding information to c file using perl

Hi,
I have c file which contains more number of tests. two or more tests in one c file. I have the XMl data regarding tests. i need to add this xml data to c file at before the test. I know which file having which test and i created hash table for that. so the problem is i have to add information to the c file at rhight place before the test. my c file look like this
Code:
/*!
*****************************************************************************
* * $b Description: Pressure Valve high
****************************************************************************/
#include "ccode.h"
/****************************************************************************/
/* Local function prototypes (static): */
void main_10ms(void)
{
test1 ( i need to add infomation before every test)
if {.....
......
if{........
.....
}else
{......
}
}
test2 
if {.....
......
if{........
.....
}else
{......
}
}

like this . i need to add information to c file using perl script. suggest me how to do this task. thanks in advance.


Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by veerubiji; 10-07-2011 at 04:12 AM..
# 2  
Old 10-05-2011
I assume that the testnames are the keys of your hash and that you need to add the value?
So read the c file and if you match any of the test names
Code:
#hint
$test_expression="(" . join("|", keys %tests).")" ;

Then print the description after printing the line
Otherwise just print the line.
Now copy the output back over the original.
This User Gave Thanks to Skrynesaver For This Post:
# 3  
Old 10-06-2011
Thanks for your reply, i will try like this and i will ask if any errors is occured.

regards,
veerubiji

---------- Post updated at 02:15 PM ---------- Previous update was at 08:43 AM ----------

Hi, i am trying like this but i am not getting result can explian little bit more. i have string names in my file like "temperature value test" i need to add information after this sting. so i read the file and i matched this string using regex but i am confusing how to add information after this.

#!/usr/bin/perl
use warnings;
use strict;
open(File, "code.c") or die "unable to open file: $!\n";
while(<File>){
if (my $string =~ /\/\*temperature value test\*\//)
push (@string,"information");
print "$_";

but i didnt get result may be there is a wrong in my code can help me how should i add information.
thanks in advance.
# 4  
Old 10-06-2011
Seriously, try using code tags when you post code, it makes everyone's life easier.

Now to address your question...
Code:
if (my $string =~ /\/\*temperature value test\*\//)

Examine that line for a moment... what does the =~ operator do? and how could $string match anything at that point?

Take a look at the following
Code:
~/tmp$ cat code.c 
#include code.h

//first test
if (test_passes()){
...
//second test
if (this_test_passes()){
..
//third test
if (this_test_passes()){

~/tmp$ cat test.pl
#!/usr/bin/perl

use strict;

my %test_description = ('first test'=>": This is the first test we undertake\n",
                        'second test'=>": This is the second test we undertake\n",
                        'third test'=>": This is the third test we undertake\n",
                       );
open (my $code , "<", 'code.c');
my $test_finder="(" . join("|", keys %test_description).")" ;
while(<$code>){
    if (/$test_finder/){
        chomp;
        $_.=$test_description{$1};
    }
    print $_
}
    
~/tmp$ ./test.pl
#include code.h

//first test: This is the first test we undertake
if (test_passes()){
...
//second test: This is the second test we undertake
if (this_test_passes()){
..
//third test: This is the third test we undertake
if (this_test_passes()){

Is that close to what you wish to do?
# 5  
Old 10-06-2011
Hi,
thanks for reply, next time i cant repeat the mistake sorry for that.
your code is very much useful for me because my output also look like but i have hash table that contains testnumber and information to add regarding testnumber. i am trying to use that hash table.
thanks in advance.

regards,
veerubiji
# 6  
Old 10-06-2011
Well, what's in this hash table, and what output would you like it to produce?
# 7  
Old 10-06-2011
Hi,
Actuvally i am trying like this, I have two hash tables.

Hash table1 contains strings and their numbers like as shown below
(value) (key)
pressure 2034
temperature 2035
humidity 2036 like that i have 100 stings and and numbers.

second hash table contains numbers and information like as shown below
(key) (value)
2034 information
2035 information
2036 information
.......................... like this, i have 100 strings and 100 unique numbers to the strings.

In my c file i have the strings names what i have in hash table1 like pressure, temperature, humidity ........

now i have to test if string matches in the c file then add according number and information before the string. this is my hole problem, i have to implement perl script for this task.

I am very beginner to perl, i am trying like this.
thanks in advance.

regards,
veerubiji.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Adding to an array in an external file, and adding elements to it.

I have an array in an external file, "array.txt", which contains: char *testarray={"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};I want to be able to add an element to this array, and have that element display, whenever I call it, without having to recompile... (29 Replies)
Discussion started by: ignatius
29 Replies

2. Shell Programming and Scripting

Perl to extract information from a file line by line

In the below perl code I am using tags within each line to extract certain information. The tags that are used are: STB >0.8 is STRAND BIAS otherwise GOOD FDP is the second number GO towards the end of the line is read into an array and the value returned is outputed, in the first line that... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Web Development

Tapatalk Modification for vB3 - Adding User for System Information

Referring back to this thread: Tapatalk Modification for vB3 - Issue with Avatar Icons I mentioned that we had some "system bot" code: In this post, I describe that code and how easy it is to create a "system bot" user for Tapatalk (TT): Basically, its quite easy. We add a hook to... (0 Replies)
Discussion started by: Neo
0 Replies

4. UNIX for Dummies Questions & Answers

Perl - adding columns to file

I have a file in which I need to add more columns to based on a key in the first file: File1 key1,abc,123, key2,def,456, key3,ghi,789, File2 key2,zyx,111,qqq, key3,yuu,222,www, key1,pui,333,eee, key4,xxx,999,rrr, I would like to create the following output: Output (1 Reply)
Discussion started by: WongSifu
1 Replies

5. Shell Programming and Scripting

Parsing information in perl

So i'm trying to write a perl script that logins into a network switch via ssh: #sh ip traffic IP statistics: Rcvd: 1460119147 total, 563943377 local destination 0 format errors, 0 checksum errors, 48401998 bad hop count 0 unknown protocol, 8379279 not a gateway ... (2 Replies)
Discussion started by: streetfighter2
2 Replies

6. Shell Programming and Scripting

perl script to print file information - newbie

Hi I have a perl script that prints all the video and audio file information(playing duration). It works fine in one of my friends linux laptop. But it doesn't work in my both windows and linux. My friend told me I have to do install some module ( ppm instal ...... ) but I have no... (1 Reply)
Discussion started by: srijith
1 Replies

7. Shell Programming and Scripting

Print file information using ffmpeg in perl

I am trying to print file information using ffmpeg tool in perl Here is my code use strict; use warnings; use IPC::Open3; # example my $filename = $ARGV; my %videoInfo = videoInfo($filename); print "duration: " . $videoInfo{'duration'} . "\n"; print "durationsecs: " .... (0 Replies)
Discussion started by: srijith
0 Replies

8. Shell Programming and Scripting

Adding new lines to a file + adding suffix to a pattern

I need some help with adding lines to file and substitute a pattern. Ok I have a file: #cat names.txt name: John Doe stationed: 1 name: Michael Sweets stationed: 41 . . . And would like to change it to: name: John Doe employed permanently stationed: 1-office (7 Replies)
Discussion started by: hemo21
7 Replies

9. Shell Programming and Scripting

cant get perl to pull information right

Hello, I cant get the perl script to pull the information from Sark DNS 4.X Options ACL Templates= and other= Can someone look at the script to see why and fix it please. FYI..Under Sark DNS 4.x ACL Templates= and other= has an indent/tab, not sure if thats the reason my the script... (24 Replies)
Discussion started by: richsark
24 Replies

10. Shell Programming and Scripting

Perl: adding columns in CSV file with information in each

Hi Wise UNIX Crew, I want to add 3 different columns to the file in which: 1. The first new column pulls in today's date and time 2. Second column one has a '0' 3. Third column has the word 'ANY' going down the column If my file content is as follows: "7","a","abc",123"... (1 Reply)
Discussion started by: dolo21taf
1 Replies
Login or Register to Ask a Question