UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old November 1st, 2004, 07:23 AM
arun0000 arun0000 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 35 arun0000 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 35 m 46 sec
Reputation Power: 4
shell scripting

hi guys. I am trying to write a small shell script which reads an input file and filters out anything that is between (// and new line) and between (*/ and /*) basically c comments. Also I am trying to count the number of "(" and ")" when encountered in the file.
I am trying to use AWK filter to accomplish this. I can use SED, Grep or any other filter if needed.

awk <search pattern> {<program actions>}

Any help would be greatly appreciated.

Thanks,
Arun

Reply With Quote
  #2  
Old November 2nd, 2004, 04:02 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 44 m 45 sec
Reputation Power: 9
between (// and new line) and between (*/ and /*)

(you sure mean: /* and */)
the fastest way doing that is sed
sed '/\/\/.*/d;/\*\/,/\/\*/d'
a probl, this will delete
/*
...
*/
AND the legal, possible and worst write style:
abc(); /*...
...
...*/ cba();

to work on this with regexpr is a big job.

the second Q: count '()' is less trivial.
sed is not appropriate (nota: no probls IF you COMMAND sed)
awk has sure a lot of functs able for that, but slow
grep can only add lines, so: xx() AND xx(); yy(); always gives 1
a combination of: sed, awk is possible, but less interesting
perl may be a good alternative
at least your friend is: a (very) simple 20 line c

Reply With Quote
  #3  
Old November 2nd, 2004, 09:09 AM
arun0000 arun0000 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 35 arun0000 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 35 m 46 sec
Reputation Power: 4
Quote:
Originally Posted by guggach
between (// and new line) and between (*/ and /*)

(you sure mean: /* and */)
the fastest way doing that is sed
sed '/\/\/.*/d;/\*\/,/\/\*/d'
a probl, this will delete
/*
...
*/
AND the legal, possible and worst write style:
abc(); /*...
...
...*/ cba();

to work on this with regexpr is a big job.

the second Q: count '()' is less trivial.
sed is not appropriate (nota: no probls IF you COMMAND sed)
awk has sure a lot of functs able for that, but slow
grep can only add lines, so: xx() AND xx(); yy(); always gives 1
a combination of: sed, awk is possible, but less interesting
perl may be a good alternative
at least your friend is: a (very) simple 20 line c


hi, thanks for the help. You are right it should be /*...*/
I can't use peral, so I have to use awk or grep. Could u pls give me an example on how to count the number of left paran and right paran.

Thanks a bunch

Reply With Quote
  #4  
Old November 2nd, 2004, 10:14 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 44 m 45 sec
Reputation Power: 9
grep is useless, and
it's a long way using awk
why do you insist with them ?
the reasonable way is c:

Code:
register int left, right;
char buf[MaxBuf], *pointer;
FILE *fd;

/*
open the file, and check, then
*/
if(!(fd = fopen("filename","r"))) exit(1);
left = right = 0;
while(fgets(buf,sizeof(buf),fd)
    for(pointer = buf; *pointer; ++pointer){
        if(*pointer == '(') ++left;
        if(*pointer == ')') ++right;
    }
fclose(fd);
printf("left = %d right = %d\n",left,right);
exit(0);

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > shell scripting


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway