How does a shell script recognize the end of a line?

Hi friends ,
I want to know how does a shell script recognize the end of a line? . i have hunddres of proccedure to test where i want to ingnore the comments which starts with "--" .. it can start from the middle of the lines also. for example::

select * from table1; -- getting all columns of a table1
 
--inserting in table2
 
insert into table1 as .... so on

so in the above example i have to ignore the comment like : "-- getting all columns of a table1" and "
--inserting in table2
" and so on

The problem is that, in my shell scripting, i am reading word by word from a file. so i can recognize from the first two character of a word (i.e "--" here)
whether it a commnet not. but how would i come to know the the end of a line so that i will stop ignoring the commnet.
for example:

cat $loopfiles | tr -s " " "\n" | while read curr_str 
do
{
 
cmt_str=`echo "$curr_str" | awk '{ printf "%s\n", substr($1,1,2) }'` 
cmt="--"
if [[ $cmt_str = $cmt ]]
then
cflag=1                       ## ignore till cflag is 1
fi
 
if [[ $curr_str = "\n" ]]
then
cflag=0                        ## stop ignoring 
fi

----------------------------------------but my shell scripting is not able to recognize the "\n" character"

please helppppppppppppppppppppppp ..

thanks a ton in advance :slight_smile:

Without understanding what your ultimate goal is this might not be the best approach; there might be better ways of processing your file. However, with what you've posted, you could preprocess the file before it's given to the tr command. Replace your useless use of cat (Useless Use of Cat Award) with a grep:

grep -v "^--"  $loopfiles | tr -s " " "\n" | while read curr_str 
 do
 {
    cmt_str=`echo "$curr_str" | awk '{ printf "%s\n", substr($1,1,2) }'` 
    cmt="--"
    if [[ $cmt_str = $cmt ]]
    then
     cflag=1 ## ignore till cflag is 1
    fi

    if [[ $curr_str = "\n" ]]
    then
      cflag=0 ## stop ignoring 
    fi

This should delete all lines from the input which start with the leading two dashes.

For future reference, your use of cat is uneeded (useless) as you can redirect the file straight into the tr command:

tr -s " " "\n" <filename

thanks AGAMA fro your reply .. but i want to ignore those commnet also which starts in the middle of a line.. for example::

select * from table1; -- getting all columns of a table1

here i want to ignore the above commnets also i.e "-- getting all columns of a table1
"

thanks in advance :slight_smile:

Ok, for some reason I read that you just wanted the comments which started at the beginning of the line.

Then use sed rather than grep:

sed '/^--/d; s/--.*//'  $loopfile | .... 

This will remove lines that start with "--" and delete all characters after the "--" on every line.

Maybe this is more what you needed.

1 Like

@agama thanks for your reply .. let me try

thanks for your solution :slight_smile: