LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed or awk question, find lines with signs and number them (http://www.linuxquestions.org/questions/linux-newbie-8/sed-or-awk-question-find-lines-with-signs-and-number-them-808382/)

Krzysztow 05-17-2010 02:09 PM

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()
{
//....code
}

So it only suffices to look for "{" signs and then insert a line with appropriate ext afterwards. In sed it's very easy to find these signs (not complicated regexp :) ), however cannot find a solution to get a line number.

Thank You for any help!

Tinkster 05-17-2010 02:25 PM

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

Krzysztow 05-17-2010 02:27 PM

Quote:

Originally Posted by Tinkster (Post 3971712)
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

Ok thanks, but my ptroblem is not not knowing how to insert a line, but the stuff I insert - either a name of the function or a line number.

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.

pixellany 05-17-2010 02:41 PM

http://www.grymoire.com/Unix/Sed.html#uh-48

More generally, that site has really good tutorials, including SED

Krzysztow 05-17-2010 02:47 PM

Quote:

Originally Posted by pixellany (Post 3971734)
http://www.grymoire.com/Unix/Sed.html#uh-48

More generally, that site has really good tutorials, including SED

I have seen that sed solution, but this prints linue numbers on each line. I want it to print only in those that have "{" sign and then finsih it with ' "; ' sign, so that I ahve
[CODE]
{qDebug()<<"something anything <line number from sed> ";
[\CODE]

Thank You!

pixellany 05-17-2010 02:51 PM

sed '/pattern/action' Based on the pattern, take the desired action---eg printing the line number.

Krzysztow 05-17-2010 06:21 PM

Quote:

Originally Posted by pixellany (Post 3971746)
sed '/pattern/action' Based on the pattern, take the desired action---eg printing the line number.

I am dwelling on this and can't do it. I want to substitute the string:
{
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.

kurwongbah 05-17-2010 08:18 PM

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;

Krzysztow 05-17-2010 08:53 PM

Quote:

Originally Posted by kurwongbah (Post 3972089)
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;

Thank You! Works for me. I have already done it in c++, but it takes more than one line of Yours :)

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!

MTK358 05-17-2010 09:06 PM

I might be able to do it in AWK, but I don't understand what you want to do.

grail 05-17-2010 09:09 PM

If it is only the line number required then awk would be simple:
Code:

awk '/^{/{gsub(/^{/,"&qDebug() <<"NR)}1' file

kurwongbah 05-17-2010 09:46 PM

Sorry couldn't help myself...

cat mycode.c | awk '/^void|int/ {funcname=$2}; /\{/ {print "{ qDebug() << " funcname " " NR; next}; {print}'

Tinkster 05-17-2010 09:59 PM

Quote:

Originally Posted by grail (Post 3972136)
If it is only the line number required then awk would be simple:
Code:

awk '/^{/{gsub(/^{/,"&qDebug() <<"NR)}1' file

If the function name is needed, too, something like
this might work.\
Code:

awk '/^{/{gsub(/^{/,"&qDebug() <<"NR" "hold)}1;{hold=$2}' file

Krzysztow 05-18-2010 11:14 AM

Thanks all of You, Guys!

Next time I need something like, I will surely use one of your solution.

All the best,
Krzysztof.

grail 05-18-2010 11:23 AM

@Tinkster - nice work :) Simple and effective ... gotta love awk


All times are GMT -5. The time now is 07:01 AM.