|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
Quote:
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 |
|
#4
|
|||
|
|||
|
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);
|
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > shell scripting |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|