LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-17-2010, 01:09 PM   #1
Krzysztow
Member
 
Registered: Apr 2010
Posts: 31

Rep: Reputation: 15
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!
 
Old 05-17-2010, 01:25 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
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
 
Old 05-17-2010, 01:27 PM   #3
Krzysztow
Member
 
Registered: Apr 2010
Posts: 31

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Tinkster View Post
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.

Last edited by Krzysztow; 05-17-2010 at 01:33 PM.
 
Old 05-17-2010, 01:41 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

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

More generally, that site has really good tutorials, including SED
 
Old 05-17-2010, 01:47 PM   #5
Krzysztow
Member
 
Registered: Apr 2010
Posts: 31

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pixellany View Post
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!
 
Old 05-17-2010, 01:51 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
sed '/pattern/action' Based on the pattern, take the desired action---eg printing the line number.
 
Old 05-17-2010, 05:21 PM   #7
Krzysztow
Member
 
Registered: Apr 2010
Posts: 31

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pixellany View Post
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.
 
Old 05-17-2010, 07:18 PM   #8
kurwongbah
Member
 
Registered: Apr 2010
Posts: 82

Rep: Reputation: 23
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;
 
Old 05-17-2010, 07:53 PM   #9
Krzysztow
Member
 
Registered: Apr 2010
Posts: 31

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by kurwongbah View Post
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!
 
Old 05-17-2010, 08:06 PM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
I might be able to do it in AWK, but I don't understand what you want to do.
 
Old 05-17-2010, 08:09 PM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,485

Rep: Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890
If it is only the line number required then awk would be simple:
Code:
awk '/^{/{gsub(/^{/,"&qDebug() <<"NR)}1' file
 
Old 05-17-2010, 08:46 PM   #12
kurwongbah
Member
 
Registered: Apr 2010
Posts: 82

Rep: Reputation: 23
Sorry couldn't help myself...

cat mycode.c | awk '/^void|int/ {funcname=$2}; /\{/ {print "{ qDebug() << " funcname " " NR; next}; {print}'
 
Old 05-17-2010, 08:59 PM   #13
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Quote:
Originally Posted by grail View Post
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
 
1 members found this post helpful.
Old 05-18-2010, 10:14 AM   #14
Krzysztow
Member
 
Registered: Apr 2010
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks all of You, Guys!

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

All the best,
Krzysztof.
 
Old 05-18-2010, 10:23 AM   #15
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,485

Rep: Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890
@Tinkster - nice work Simple and effective ... gotta love awk
 
  


Reply

Tags
sed


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
Inserting Multiple Lines (with newline) using sed or awk hal8000b Programming 1 03-08-2009 05:21 PM
Delete specific Range of lines Using sed , awk, grep etc. joyds219 Linux - Newbie 4 03-28-2008 08:59 AM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 09:00 AM
find awk sed.. something along these lines citrus Linux - General 1 08-21-2006 03:04 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM


All times are GMT -5. The time now is 04:24 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