LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   need to write a shell script for appending text into each line of a file (https://www.linuxquestions.org/questions/programming-9/need-to-write-a-shell-script-for-appending-text-into-each-line-of-a-file-747235/)

nandakv4 08-13-2009 04:25 AM

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,,

jeromeNP7 08-13-2009 06:31 AM

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

sycamorex 08-13-2009 06:52 AM

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

nandakv4 08-13-2009 11:27 PM

Quote:

Originally Posted by jeromeNP7 (Post 3641718)
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!!

nandakv4 08-13-2009 11:33 PM

Quote:

Originally Posted by sycamorex (Post 3641731)
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...

chrism01 08-14-2009 12:38 AM

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

catkin 08-14-2009 01:49 AM

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. :twocents:

nandakv4 08-15-2009 12:04 AM

Quote:

Originally Posted by chrism01 (Post 3642814)
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..

nandakv4 08-15-2009 12:07 AM

Quote:

Originally Posted by catkin (Post 3642850)
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. :twocents:

I am ignorant on how to employ trace utilities :( .
Know nothing about them..
And also what you mean 'compiling with symbols'??

sycamorex 08-15-2009 03:32 AM

Quote:

Originally Posted by nandakv4 (Post 3644027)
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?


All times are GMT -5. The time now is 12:15 AM.