LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-13-2009, 04:25 AM   #1
nandakv4
LQ Newbie
 
Registered: Aug 2009
Posts: 5

Rep: Reputation: 0
need to write a shell script for appending text into each line of a file


hello,

I have a huge source code of a software in front of me and need to find out the flow of execution. It has many C files that comprise it and its normal execution involves passing values from one code to the other.

what i want is a shell script that will enter into the C prog file and append a string such as '; printf("%s %d",$file_name,$line_number); '
at each line..

this will help me understand the flow of the program.
can anyone suggest a shell script to do this?? i use ubuntu..


thanks,,
 
Old 08-13-2009, 06:31 AM   #2
jeromeNP7
Member
 
Registered: Jun 2009
Posts: 101

Rep: Reputation: 19
The syntax "printf("%s %d",$file_name,$line_number); " indicates that you are used to scripting languages like PHP. You won't have $file_name in C. Depending on your compiler you use macros like __FILE__ or __LINE__ for the same purpose. If you'r doing programming anyway, you need to create a program using fgets to read each line of the source code, stripping the trailing linefeed with something like s[strlen(s -1)] = 0, making sure that your buffer is large enough for very long lines, and the outputting the line plus the printf statement followed by a linefeed to a new copy of the source code. A good programming exercise, shouldn't take long to write.

Linux

Last edited by jeromeNP7; 09-04-2009 at 09:32 PM.
 
Old 08-13-2009, 06:52 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,561
Blog Entries: 1

Rep: Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023
I know VERY little about C, but it doesn't seem right to me to add '; blahblahblah' to EVERY signle line of a C source file. What about a line which declares a function, eg:
Quote:
int main()
Would the code still work?
As I said I don't know much about C and am not learning it so I might be completely wrong, it just seems not right to me.

Anyway, as far as SED is concerned, it would look like:
sed 's/$/staff_to_put_at_the_end_of_the_line/g' name_of_the_file

For example, if you wanted to append '; printf();' to the end of each line in the file: program.c, it would look as follows;

Code:
sed 's/$/; printf();/g' program.c
 
Old 08-13-2009, 11:27 PM   #4
nandakv4
LQ Newbie
 
Registered: Aug 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by jeromeNP7 View Post
The syntax "printf("%s %d",$file_name,$line_number); " indicates that you are used to scripting languages like PHP. You won't have $file_name in C. Depending on your compiler you use macros like __FILE__ or __LINE__ for the same purpose. If you'r doing programming anyway, you need to create a program using fgets to read each line of the source code, stripping the trailing linefeed with something like s[strlen(s -1)] = 0, making sure that your buffer is large enough for very long lines, and the outputting the line plus the printf statement followed by a linefeed to a new copy of the source code. A good programming exercise, shouldn't take long to write.
oh yes, am transitioning from TCl to C, so mistakenly put a $.
anyway, i think i will look up on those macros you suggested, and will experiment with fgets as mentioned.
thanks!!
 
Old 08-13-2009, 11:33 PM   #5
nandakv4
LQ Newbie
 
Registered: Aug 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by sycamorex View Post
I know VERY little about C, but it doesn't seem right to me to add '; blahblahblah' to EVERY signle line of a C source file. What about a line which declares a function, eg:

Would the code still work?
As I said I don't know much about C and am not learning it so I might be completely wrong, it just seems not right to me.

Anyway, as far as SED is concerned, it would look like:
sed 's/$/staff_to_put_at_the_end_of_the_line/g' name_of_the_file

For example, if you wanted to append '; printf();' to the end of each line in the file: program.c, it would look as follows;

Code:
sed 's/$/; printf();/g' program.c
yes, there are some drawbacks if i append a printf for each line, for example, if the line is empty and out of a function scope, it will error out! will have to figure out a way to skip such occurrences..

and i didn't get your 'int main()' utility here,,

thanks for the sed usage fromat!! i ll have to figure out how to loop and insert line numbers in them, probably the macro __LINE__ should help...

Last edited by nandakv4; 08-13-2009 at 11:49 PM.
 
Old 08-14-2009, 12:38 AM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Code:
int main(void) 

OR

int main(int argc, char *argv[])
is the start of every C prog:
http://en.wikipedia.org/wiki/Main_fu...#C_and_C.2B.2B
 
Old 08-14-2009, 01:49 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
How about leaving the program as-is, compiling it with symbols not stripped (if it's not already) and using a tracing utility? Just a suggestion; not my area of knowledge.
 
Old 08-15-2009, 12:04 AM   #8
nandakv4
LQ Newbie
 
Registered: Aug 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
Code:
int main(void) 

OR

int main(int argc, char *argv[])
is the start of every C prog:
http://en.wikipedia.org/wiki/Main_fu...#C_and_C.2B.2B
yes sir,
i am aware of this.
I do not know why it was suggested to me here to solve my problem..
 
Old 08-15-2009, 12:07 AM   #9
nandakv4
LQ Newbie
 
Registered: Aug 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
How about leaving the program as-is, compiling it with symbols not stripped (if it's not already) and using a tracing utility? Just a suggestion; not my area of knowledge.
I am ignorant on how to employ trace utilities .
Know nothing about them..
And also what you mean 'compiling with symbols'??
 
Old 08-15-2009, 03:32 AM   #10
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,561
Blog Entries: 1

Rep: Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023
Quote:
Originally Posted by nandakv4 View Post
yes sir,
i am aware of this.
I do not know why it was suggested to me here to solve my problem..
I wasn't trying to teach you what 'int main' is or that it's the solution to your problem
I was simply pointing out that appending something like ';printf.....' to EACH line of C code would mess things up. As, FOR EXAMPLE:

Code:
int main () ; printf("%s %d",$file_name,$line_number);
... would be an illigal statement in C as after 'int main()' a '{' is expected.

At least that's what my limited knowledge of C syntax tells me Am I correct?
 
  


Reply

Tags
script, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A shell script to include a message on line 2 of every text file in a director tree? Rotwang Linux - General 4 02-26-2009 05:01 PM
shell script loop though text file:-1 line at a time. knockout_artist Linux - Newbie 2 05-04-2008 06:58 PM
How to read a single line from a text file into a shell script. SkipHuffman Linux - Software 2 08-16-2006 02:10 PM
Shell script - how to show a specific line of a text file davi_cabral Linux - Software 3 09-28-2004 01:39 PM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM


All times are GMT -5. The time now is 07:09 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration