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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-21-2009, 06:24 AM
|
#1
|
Member
Registered: Oct 2009
Posts: 40
Rep:
|
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?
|
|
|
11-21-2009, 06:29 AM
|
#2
|
Senior Member
Registered: Sep 2007
Posts: 1,047
Rep:
|
What are you trying to do?
Describe with example.
|
|
|
11-21-2009, 06:47 AM
|
#3
|
Member
Registered: Oct 2009
Posts: 40
Original Poster
Rep:
|
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?
|
|
|
11-21-2009, 07:15 AM
|
#4
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
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
|
|
|
11-21-2009, 08:09 AM
|
#5
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
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?
|
|
|
11-21-2009, 08:51 AM
|
#6
|
Member
Registered: Oct 2009
Posts: 40
Original Poster
Rep:
|
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
|
|
|
11-21-2009, 09:10 AM
|
#7
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
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
|
|
|
11-23-2009, 08:45 AM
|
#8
|
Member
Registered: Oct 2009
Posts: 40
Original Poster
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 05:48 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|