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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How does a shell script recognize the end of a line?
# 1  
Old 05-06-2012
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::

Code:
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:


Code:
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 Smilie

Last edited by Scrutinizer; 05-06-2012 at 12:13 PM.. Reason: code tags
# 2  
Old 05-06-2012
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:

Code:
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:
Code:
tr -s " " "\n" <filename


Last edited by agama; 05-06-2012 at 12:11 PM.. Reason: clarification
# 3  
Old 05-06-2012
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 Smilie
# 4  
Old 05-06-2012
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:

Code:
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.
This User Gave Thanks to agama For This Post:
# 5  
Old 05-06-2012
@agama thanks for your reply .. let me try
# 6  
Old 05-06-2012
thanks for your solution Smilie

Last edited by Corona688; 05-06-2012 at 02:05 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to check line end not ending with comma

I have several line in a text file. for example I like apple; I like apple I like orange; Output: I like apple I try to useif grep -q "!\;$"; then (Not work) Please use CODE tags when displaying sample input, sample output, and code segments (as required by forum rules). (1 Reply)
Discussion started by: cmdcmd
1 Replies

2. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

3. Shell Programming and Scripting

my shell now adds extra space at end of each line!

Hi, Since today, with csh or tcsh, if I do 'ls files* > list', every lines end with an extra space! What happenned? What can I do to go back when there was no extra space? If I change to bash, there's no extra space. Thanks, Patrick ---------- Post updated at 03:19 PM... (1 Reply)
Discussion started by: trogne
1 Replies

4. Shell Programming and Scripting

help needed with shell script to append to the end of a specific line in a file on multiple servers

Hi Folks, I was given a task to append three IP's at the end of a specific (and unique) line within a file on multiple servers. I was not able to do that with the help of a script. All I could was: for i in server1 server2 server3 server4 do ssh $i done I know 'sed' could be used to... (5 Replies)
Discussion started by: momin
5 Replies

5. Shell Programming and Scripting

sybase shell line 19: unexpected end of file

Hi, As i am new to this forum i dont really know where to place this doubt i am facing ,Please consider this In this code i am connecting two time to two different server / database and getting the .sql file where am i going wrong please guide..... me. thanks a lot if ; then echo... (2 Replies)
Discussion started by: oracle_coorgi
2 Replies

6. Shell Programming and Scripting

Help with shell script: moving end of line character

Hello. I have a file (old.txt) that I need to copy into another file (new.txt). Each line on old.txt ends with CR/LF but the position of CR/LF varies from one record to another. I need to copy each line of record to new.txt and move CR/LF in pos 165. Can I use awk to achieve this? How?... (8 Replies)
Discussion started by: udelalv
8 Replies

7. Shell Programming and Scripting

Help on shell script : syntax error at line 62: `end of file' unexpected

Hi All, I have written a korn script (code pasted below). It is giving the error while debugging "new.sh: syntax error at line 62: `end of file' unexpected". I have re-written the whole code in VI and explored all help related to this error on this Unix forum and tried it. Somehow, I could... (7 Replies)
Discussion started by: schandrakar1
7 Replies

8. Shell Programming and Scripting

add a line at the end of a file using shell scripting

Hi All , I am new to shell scripting. i have a shell script(which is executed as a super user) , i want to it to do one more job for me. ie mounting a directory over other using lofs . i have done it manually using 1) using mount command mount -F lofs /export/home/dju /dju 2)... (4 Replies)
Discussion started by: meet123321
4 Replies

9. Shell Programming and Scripting

End of Line in Shell Scripting

I have a file containing records seperated by delimiter '|'.A record is contained on 2 or 3 lines. Now I need a shell script which could write all records with in two '|' character in one line.....for example..... |R1........................R1 R1..........................R1... (2 Replies)
Discussion started by: 33junaid
2 Replies

10. Shell Programming and Scripting

shell script cant recognize if else compare

hi I face the problem the if else statement dint return correct result for me my script as below: #!/bin/ksh sqlplus -s /nolog <<EOF connect databaseuser/password column num new_value num format 9999 set head off select count(*) num from table1; exit num EOF if ; then echo "$?"... (6 Replies)
Discussion started by: jaseloh
6 Replies
Login or Register to Ask a Question