Home > Back-end >  bash- iterate over files and take only the cases
bash- iterate over files and take only the cases

Time:08-31

I'm trying to write a bash script which iterates over all files in directory and takes all the cases inside- all the code which is between 'case' and 'endcase'

this is what I wrote:

for FILE in <directory_path>/*.sv
do
    
    cat $FILE | while read line 
        do 
            if [[ $line == *"case"* && $line != *"//"* ]] 
                then
                    indicator_to_copy_row=1
            
            else if [[ $line == *"endcase"* && $line != *"//"* ]] 
                then
                    indicator_to_copy_row=0
            fi
            fi

            if (($indicator_to_copy_row == 1)) 
                then
                    echo $line >> txt_path
                     
            fi
        done
done

it did take all the cases as I wanted, but also took a few lines of the code which come after the endcase statement

does anyone identifies the problem? any other thoughts? thanks

CodePudding user response:

Looks like you are simply looking for

sed -n '\%//%d;/case/,/endcase/p' path/*.sv

CodePudding user response:

I don't know, this shouldn't be a debugging service. Here's some tips:

Instead of opening the output file thousands of times, do it once...

for FILE in blah/*.sv
do
  cat $FILE | while read line
  do
. ... etc
  done
done > txt_path

now add debug statements to your code...

            if (($indicator_to_copy_row == 1)) 
                then
                    echo itcr:$indicator_to_copy_row $line
                     
            fi

now you're on your way to debugging your own code.

  •  Tags:  
  • bash
  • Related