Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a file

1.txt

ejryheiur

rewurie

(

ejwhrejkrh

rewrhwe

rewkhrwe

rewhkr

)

dfhgsdfh

rqwergjweg

jhwgrh

(

rehgwrwhe


rhwejkr

)

With this nawk script

   nawk '
       BEGIN { 
       count1=1  ;
       count2=0 ;
       }
       {
       if ((NR > 2) && ( $0 ~/(/) ) 
       {
            count1++ ;
            if ((NR > 2) && ($0 ~/)/)) 
            {
                count2++ ; 
                if (count1 == count2) 
                {
                  print NR ; 
                  exit 
                }
            }
        }
        else 
        {
            if ((NR > 2) && ($0 ~ /)/)) 
            {
                count2++ ; 
                if (count1 == count2) 
                {
                    print NR;
                    exit
                }
            }
        }
       }
   ' 1.txt

But I'm getting this error:

nawk: illegal primary in regular expression ( at source line number 7 context is

if ((NR > 2) && ($0 >>>  ~/(/ <<< ))
share|improve this question
2  
This would be a lot easier to help with if you put your script into a text file and did a little formatting. – BillP3rd Feb 21 '14 at 5:43
    
Helping for free, Don't we deserve a bit more explanation i.e. //Quotes in the code ?? =P also Format your answer. Too hard to read.. – Pathfinder Feb 21 '14 at 7:21
    
sorry for the inconvenience , can you please help me – Timson Feb 21 '14 at 10:07
    
Did some additional formatting fixes... – nerdwaller Feb 21 '14 at 12:38

You need to escape the round bracket in the regular expression.

Either with a backslash:

if ((NR > 2) && ( $0 ~/\(/) )

Or with square brackets:

if ((NR > 2) && ( $0 ~/[(]/) )
share|improve this answer
    
@Timson, not the answer you were looking for? We can flesh this answer out, if you still have questions. – UnlimitedInfinity Feb 28 '14 at 10:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .