LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-21-2009, 06:24 AM   #1
carolflb
Member
 
Registered: Oct 2009
Posts: 40

Rep: Reputation: 15
GREP: how to specify a line or lines where to search


Hi guys,

I need to find a term but just between certain lines, so I don't want the command 'grep' to search through the whole text file. Is there a way to do that using 'grep'?

If not, would somebody know another way to do that?
 
Old 11-21-2009, 06:29 AM   #2
cola
Senior Member
 
Registered: Sep 2007
Posts: 1,047

Rep: Reputation: 65
What are you trying to do?
Describe with example.
 
Old 11-21-2009, 06:47 AM   #3
carolflb
Member
 
Registered: Oct 2009
Posts: 40

Original Poster
Rep: Reputation: 15
Hello cola,

I have a .h file where I need to know where all IF-ELSE statements begin and end. I need to do that because I'm counting variables and if a variable is inside of an IF-ELSE I should just count it once, since just the IF or just the ELSE will be runned at a time. So if I have for example:

if (smt) {
var=true;
}
else{
var=false;
}


I need to know where the if begins, where the else begins, but also where the else ends (this is the hard part for me). I tough I can mark it if I try to find the first "{" and the last "}" after the ELSE, to solve this. But since I also need to consider such scenarios:

...more code

if (smt){ ... }
if (smt){ ... }
else { ...
if { ... }
else { ... }
}


That means there might be other IF-ELSEs inside of an IF or ELSE, what means more "{" and "}" that won't be the end of the ELSE I'm looking for. Any idea how to solve this?
 
Old 11-21-2009, 07:15 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
You might want to look at the addressing feature in SED.
Example:
Code:
sed -n '/start/,/end/p' filename
This returns all lines from one containing "start" up through one containing "end". You might need more that one chained together to satisfy all of your conditions.

Really goos SED tutorial here:
http://www.grymoire.com/Unix/Sed.html
 
Old 11-21-2009, 08:09 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
i think its not that simple just using grep or sed. what if you have nested if else, or if without else. how are you going to count? do you always want to find if/else pairs??
you should describe more clearly what you want to do. If you do not care about these, then grep for "if" and count how many are there should be fine right?
 
Old 11-21-2009, 08:51 AM   #6
carolflb
Member
 
Registered: Oct 2009
Posts: 40

Original Poster
Rep: Reputation: 15
Yes guys,

I also think that 'sed' wouldn't help and with 'grep' it is also lots of work... but I'm trying to find a way marking the 'IF', 'ELSE' and 'brackets' I already found, so that once I run 'grep' I can find the next one.

@ghostdog: What I need to do is to count how many times an specific variable is USED in the .h file (not counting de declaration). So if we have:

bool var=false;

if (smt){ var=true; }

if (smt) {
var=true;
}
else{
var=false;
}

...more code

if (smt){ ... }

...more code

if (smt){
var=true;
}
else {
if {
var=false;
}
else {
var=true;
}
}


The variable counter needs to give me '3' (colored in pink), since the others are inside IF-ELSEs. The 'var' is changed just one time for each IF-ELSE once the code runs. I hope that now it is clear enough
 
Old 11-21-2009, 09:10 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
as usual, a gawk solution.
Code:
awk 'NR{
 m=split($0,e,"else")
 for(i=1;i<=m;i++){    
    if( e[i] ~ /if.*var=true/){
        print "$ ==> "e[i]
        ++count
    }
 } 
}END{
    print "total: "count
}' RS= file
output
Code:
$ ./shell.sh
$ ==> if (smt){ var=true; }
$ ==> if (smt) {
var=true;
}

$ ==> if (smt){
var=true;
}

total: 3
 
Old 11-23-2009, 08:45 AM   #8
carolflb
Member
 
Registered: Oct 2009
Posts: 40

Original Poster
Rep: Reputation: 15
Hi ghostdog,

I just tried your solution, but it just counts the 'var's inside of IFs. What could I add in your code so that it also counts the 'var's outside IFs but in the .h file?

Ah! And another question, I saw that I can set a variable for the program in awk, but how do I make to receive a result from the program? I would like to keep the 'count' to use some where else.
 
  


Reply


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
grep lines before and after magnum81 Programming 3 07-29-2011 04:52 PM
How to grep lines containing a certain string PLUS the line following that line? kmkocot Linux - Newbie 5 09-01-2009 03:54 PM
shell script find a line and the next line (grep?) metalx1000 Programming 5 07-24-2007 08:41 PM
Getting only unique lines using grep.. sph90457 Linux - Newbie 6 07-05-2007 08:31 AM
Grep lines gsibble Linux - General 1 04-12-2004 01:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:48 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration