sed or awk question, find lines with signs and number them
Hi everyone,
I need a soution to my problem. I tried to look for it in some tutorials, but couldn't get out how to do it exactly. My problem is that I have to find in my code where exactly (whichc function) segmentation fault happens (c++). So the esasiest solution is to print on the console the name of the function, once the code gets into it. However until now I got quite a big bunch of functions, and don't want to do it manually. So I have to search through few files and once the function occurs, I insert there a name of the function or (file name and line name) with appropriate text qDebug()<< <fct name + line # >. Please give me advice how to do it. I write functions in a following manner: Code:
type fctName() Thank You for any help! |
Ummm ... if this is for debugging and just a temporary measure ...
why BOTHER with the new line? Just use sed to replace { with { cout << Debug stuff |
Quote:
So You say to replace "{" with "{ cout << Debug stuff ", but I need "{cout << Debug stuff <line #>" and that is a problem. Thanks anyway! Or whatever, I just one to make these inserted lines unique, so that I know what the last invoked funciton was. |
http://www.grymoire.com/Unix/Sed.html#uh-48
More generally, that site has really good tutorials, including SED |
Quote:
[CODE] {qDebug()<<"something anything <line number from sed> "; [\CODE] Thank You! |
sed '/pattern/action' Based on the pattern, take the desired action---eg printing the line number.
|
Quote:
{ with {qDebug()<<"="; so there is a constant string to be added and at the end =, which should give a line number, then "; again. I tried everything, what I could. I can't do it. It is too hard to me. In fact I spend so much time on it, looking for, that I could have done it different ways. It seems to me that You can't even insert a line number at the end of this line. Beginning is possible, cause one can just get rid of \n sign. Otherwise not. |
In Perl you can do a few tricks, maybe it's of some use for your problem!?
cat mycode.c | perl -n -e'/^void|int\s+(\w+)/; $funcname=$1; if (/^\{/) {print "{ qDebug() << $funcname $.\n"} else {print}' Converts: #Comment1 # Comment2 void func1 { some code1_1 some code1_2 some code1_3 some code1_4 } # kjhjkh int func2 { some code2_1 some code2_2 some code2_3 some code2_4 } #Comment3 some code; Into: #Comment1 # Comment2 void func1 { qDebug() << func1 5 some code1_1 some code1_2 some code1_3 some code1_4 } # kjhjkh int func2 { qDebug() << func2 15 some code2_1 some code2_2 some code2_3 some code2_4 } #Comment3 some code; |
Quote:
Your solution is the one I expected. But still, just being curious, maybe someone knows how to do it just using sed or awk? All the best! |
I might be able to do it in AWK, but I don't understand what you want to do.
|
If it is only the line number required then awk would be simple:
Code:
awk '/^{/{gsub(/^{/,"&qDebug() <<"NR)}1' file |
Sorry couldn't help myself...
cat mycode.c | awk '/^void|int/ {funcname=$2}; /\{/ {print "{ qDebug() << " funcname " " NR; next}; {print}' |
Quote:
this might work.\ Code:
awk '/^{/{gsub(/^{/,"&qDebug() <<"NR" "hold)}1;{hold=$2}' file |
Thanks all of You, Guys!
Next time I need something like, I will surely use one of your solution. All the best, Krzysztof. |
@Tinkster - nice work :) Simple and effective ... gotta love awk
|
All times are GMT -5. The time now is 06:56 AM. |