Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 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 at 7:21
    
sorry for the inconvenience , can you please help me –  Timson Feb 21 at 10:07
    
Did some additional formatting fixes... –  nerdwaller Feb 21 at 12:38
add comment

1 Answer 1

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 at 10:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.