File lines starts with # not processed or exclude that lines from processing

I have a file like below

#Fields section bald
1234 2345 456 222
abcs dddd dddd ssss
mmmm mmm mmm

i need do not process a files stating with #

I was written code below

while read -r line
 do
 
          if [ egrep -v '^#'  $file]
          then
         echo ${line} >>
         elif [ $scode -le 399 ]
          then
          echo ${line} >>access.log
          else
         echo ${line}>>error.log
done > $file

it processing but not expectings
and also tried below like

           while read -r line
 do
 
          if [ egrep -v '^#'  $file]
          then
         echo ${line} >>
         elif [ $scode -le 399 ]
          then
          echo ${line} >>access.log
          else
         echo ${line}>>error.log
done > `egrep -v '^#'  $file`

it is processing but not going to specific file which i am expecting to access and error.logs

Please correct me where should i correct my script.

both are not working as expected

You still have missing spaces.

2 Likes

Thanks for update

where is missing the space

Hi egrep works on an entire file, that is not what you are looking for. You could try this:

while read -r line
do
  case $line in 
    (\#*) continue
  esac
  printf "%s\n" "$line"
  .....
done < yourfile

Where do you set scode ?