954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

parsiring asterisk

Hi all,
I am writing a script which parses /* to find out the commented portion of the code.
I am using grep "/\*" file.c
Following is the code snippet

for i in `grep "/\*" file.c`; do
.
.
.
done

but this is not giving the lines greped from file.c.
However if I run this command from the shell i.e.
grep "/\*" file.c I get the correct answer, I think the problem is due to ` .
Can some body help me on this

TIA
tan...

tanalam
Newbie Poster
5 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

Try grep ///*

ithelp
Nearly a Posting Maven
Banned
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
 

That too is not working.

Actually in the same script file if I do only
grep "/\*" file.c
it works fine
But when I put it under the single quote ` ` the behavior changes

tanalam
Newbie Poster
5 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 
for i in `grep "/\\*" file.c`; do


You need to escape the escape character when doing this inside of the expression quotes.

Edit: Which, I believe, is what ithelp meant, but he mistakenly used /// instead of /\\.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Thanks for reply
but that thing is still not working for me.
Here is the code
for i in `grep "/\\*" file `; do echo ">>>$i ";do
echo "$i"
done

Contents of file is simple
Line 1
LIne 2
/*
//
LIne 3

The output(mentioned below) is strange listing of / directory
/bin
/boot
/dev
/etc
/home
/initrd
/lib
/lost+found
/media
/misc
/mnt
/opt
/proc
/root
/sbin
/selinux
/srv
/sys
/tmp
/usr
/var

regards
tan...

tanalam
Newbie Poster
5 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

It is because of file name expansion in the shell.

Just for fun try it this way

for i in `grep "/\\*" file`; do
  set -f i=$i
  echo "$i"
done
masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

for i in `grep "/\\*" file`; do
set -f i=$i
echo "$i"
done

is working on the shell prompt but not from the shell script. It is still giving the same output

tanalam
Newbie Poster
5 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

Ok, unfortunately, the only thing left to do, that I can think of, is to do the script this way:

#!/bin/sh -f
for i in `grep "/\\*" file`; do
  echo "$i"
done


And start the entire script without file name generation. That means, however, that file name expansion/generation does not work at all in the script, which may be wholly unenjoyable, but at least this part works.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Thanx "#!/bin/sh -f" works
Also I found one more way to make it work
. ./script.sh
that is running the script from the same shell.
I wonder if both are the same method or different one.

tanalam
Newbie Poster
5 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

Different. Although I don't really know why sourcing the script, rather than simply running it, would make a difference in the way set -f acts, but, oh well.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You