finding the second occurrence via grep

finding the second occurrence via grep

Post by justin.p... » Thu, 29 Jun 2006 02:51:07



Hi, I'm very new in Unix and would like to ask a fairly elementary
question.  I'm writing a C Shell script that automatically inserts a
new line in a certain place in a C++ or IDL file.  This line's
information is irrelevant, but my first step is finding the second
occurrence of an end comment "*/".  Once it finds this line, it would
insert a blank line above this one.

Any help would be greatly appreciated.  Sorry if this is very n00bish
but I am very new to the Unix/Linux world.  Thanks!

Justin

 
 
 

finding the second occurrence via grep

Post by Michael Tosc » Thu, 29 Jun 2006 05:17:59



> Hi, I'm very new in Unix and would like to ask a fairly elementary
> question.  I'm writing a C Shell script that automatically inserts a
> new line in a certain place in a C++ or IDL file.  This line's
> information is irrelevant, but my first step is finding the second
> occurrence of an end comment "*/".  Once it finds this line, it would
> insert a blank line above this one.

> Any help would be greatly appreciated.  Sorry if this is very n00bish
> but I am very new to the Unix/Linux world.  Thanks!

> Justin

1. Never use C shell!
2. grep can only show lines but not insert a line!

Here is a B shell that reads oldfile and creates newfile,
inserting an empty line before the 2nd occurrence of */ :

#!/bin/sh
(
cat oldfile
echo ""
) |
(
cnt=0
first=1
while read line
do
  if [ $first -eq 0 ]; then
   echo "$oldline"
  else
   first=0
  fi
  oldline="$line"
  case "$line" in
  *\*/*)
   cnt=`expr $cnt + 1`
   if [ $cnt -eq 2 ]; then
    echo ""
   fi
   ;;
  esac
done
) > newfile

--


 
 
 

finding the second occurrence via grep

Post by Chris F.A. Johnso » Thu, 29 Jun 2006 05:47:27



> Hi, I'm very new in Unix and would like to ask a fairly elementary
> question.  I'm writing a C Shell script that automatically inserts a
> new line in a certain place in a C++ or IDL file.  This line's
> information is irrelevant, but my first step is finding the second
> occurrence of an end comment "*/".  Once it finds this line, it would
> insert a blank line above this one.

   First, don't use C shell for scripting. See:

        <http://www.grymoire.com/Unix/CshTop10.txt>
        <http://www.grymoire.com/Unix/Csh.html#uh-0>
        <http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/>

   Second, grep is the wrong tool. Awk is probably best for this job:

awk  '/\*\// { ++x; if ( x == 2 ) print "" } {print}' FILENAME

--
   Chris F.A. Johnson, author              <http://cfaj.freeshell.org>
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   ===== My code in this post, if any, assumes the POSIX locale
   ===== and is released under the GNU General Public Licence

 
 
 

finding the second occurrence via grep

Post by Michael Tosc » Thu, 29 Jun 2006 06:18:18




>> Hi, I'm very new in Unix and would like to ask a fairly elementary
>> question.  I'm writing a C Shell script that automatically inserts a
>> new line in a certain place in a C++ or IDL file.  This line's
>> information is irrelevant, but my first step is finding the second
>> occurrence of an end comment "*/".  Once it finds this line, it would
>> insert a blank line above this one.

>    First, don't use C shell for scripting. See:

>         <http://www.grymoire.com/Unix/CshTop10.txt>
>         <http://www.grymoire.com/Unix/Csh.html#uh-0>
>         <http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/>

>    Second, grep is the wrong tool. Awk is probably best for this job:

> awk  '/\*\// { ++x; if ( x == 2 ) print "" } {print}' FILENAME

and even shorter is

awk  '/\*\// && ++x==2 {print ""} {print}' FILENAME

--

 
 
 

finding the second occurrence via grep

Post by Chris F.A. Johnso » Thu, 29 Jun 2006 07:06:47





>>> Hi, I'm very new in Unix and would like to ask a fairly elementary
>>> question.  I'm writing a C Shell script that automatically inserts a
>>> new line in a certain place in a C++ or IDL file.  This line's
>>> information is irrelevant, but my first step is finding the second
>>> occurrence of an end comment "*/".  Once it finds this line, it would
>>> insert a blank line above this one.

>>    First, don't use C shell for scripting. See:

>>         <http://www.grymoire.com/Unix/CshTop10.txt>
>>         <http://www.grymoire.com/Unix/Csh.html#uh-0>
>>         <http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/>

>>    Second, grep is the wrong tool. Awk is probably best for this job:

>> awk  '/\*\// { ++x; if ( x == 2 ) print "" } {print}' FILENAME

> and even shorter is

> awk  '/\*\// && ++x==2 {print ""} {print}' FILENAME

    Shorter still (but I wouldn't use it):

awk  '/\*\// && ++x==2 {print ""} 1' FILENAME

--
   Chris F.A. Johnson, author              <http://cfaj.freeshell.org>
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   ===== My code in this post, if any, assumes the POSIX locale
   ===== and is released under the GNU General Public Licence

 
 
 

finding the second occurrence via grep

Post by dJYanste » Thu, 29 Jun 2006 21:33:58


Thanks for all your help, it is certainly appreciated!

Best,
Justin

 
 
 

1. grepping n occurrences of a char in a line

Hello, I wish to match lines in a file that have exactly n occurrences of
a given character, not necessarily adjacent. Can grep do this in one pass?

Also, I wish to match lines in a file that have _at_least_ n occurrences
of a given character.

Thank you for any hints. Best regards - Olaf

2. Missing Libraries with kde2

3. how to let grep terminate once grab the first or the last occurrence?

4. X on laptop?

5. Find the first occurrence of a timestamp within a file

6. lllll DON'T READ THIS llllll

7. Finding single occurrences of a quote using regexp

8. sreen blanking and monitor standby mode

9. Find the last occurrence of "/"

10. sed: find 2nd last occurrence

11. Finding occurrences of text in multiple directories?

12. grep sTerm * | grep -v grep ???

13. tail | grep | grep | grep