Reading line in while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading line in while loop
# 1  
Old 09-27-2013
Reading line in while loop

Hello Team,
i have to read line by line in a while loop, and the input file has:.
Code:
 # The script will start cppunit test application to run unit tests.
-LD_LIBRARY_PATH=$CPPUNIT_HOME/lib:\
+LD_LIBRARY_PATH=$VOBTAG/SS_UnitTest/lib:\
 $VOBTAG/SS_BFD/BFDSCLI/build:\
 $VOBTAG/SS_LibPMGMT/PmgmtCAPI/build:\
 $VOBTAG/SS_LibPMGMT/PmgmtCppAPI/build:\
Index: SS_BFD/TXLDAPPlugIn/tst/cppunit/src/Main.cpp
===================================================================
--- SS_BFD/TXLDAPPlugIn/tst/cppunit/src/Main.cpp        (.../SS_BFD_4.76)       (revision 1970)
+++ SS_BFD/TXLDAPPlugIn/tst/cppunit/src/Main.cpp        (.../SS_BFD_4.82)       (revision 1970)
@@ -1,4 +1,5 @@
 #include <iostream>
+#include <cstdlib>

but my below code is generating the output with "$VOBTAG" 3 lines in a single line.
Code:
#!/bin/bash
while read line
  do
    echo $line
    echo "*****************"
  done<$1

Output:
Code:
*****************

*****************
# The script will start cppunit test application to run unit tests.
*****************
-LD_LIBRARY_PATH=$CPPUNIT_HOME/lib:+LD_LIBRARY_PATH=$VOBTAG/SS_UnitTest/lib: $VOBTAG/SS_BFD/BFDSCLI/build: $VOBTAG/SS_LibPMGMT/PmgmtCAPI/build: $VOBTAG/SS_LibPMGMT/PmgmtCppAPI/build:Index: SS_BFD/TXLDAPPlugIn/tst/cppunit/src/Main.cpp
*****************
===================================================================
*****************
--- SS_BFD/TXLDAPPlugIn/tst/cppunit/src/Main.cpp (.../SS_BFD_4.76) (revision 1970)
*****************
+++ SS_BFD/TXLDAPPlugIn/tst/cppunit/src/Main.cpp (.../SS_BFD_4.82) (revision 1970)
*****************
@@ -1,4 +1,5 @@
*****************
#include <iostream>
*****************
+#include <cstdlib>
*****************

Could you pls let me know how i can read each line separately , the input file being svn diff, i don want to consider with what the line is ending(Ex. \).

Regards,
Chandana
# 2  
Old 09-27-2013
Try:
Code:
while IFS='' read -r line
do	printf "%s\n*****************\n" "$line"
done < "$1"


Last edited by Don Cragun; 09-27-2013 at 08:22 AM.. Reason: Use $1 to name input file.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-27-2013
Hi Chandana,
Could you pls let me know how i can read each line separately , the input file being svn diff, i don want to consider with what the line is ending(Ex. \).


actually your script reads each line separately and prints it along with that "********" line.

i don want to consider with what the line is ending - you mean, you dont want to read lines ending with "\", you wanted to read other lines only.. is that correct?
# 4  
Old 09-27-2013
Quote:
Originally Posted by inventsekar
... ... ...
Quote:
actually your script reads each line separately and prints it along with that "********" line.
... ... ...
Actually the original script removes leading spaces and tabs; if a line ends with \, the backslash is discarded and the following line is added to the current line (repeating until a line does not end with \); removes trailing spaces and tabs; and converts any sequence of one or more spaces and tabs to a single space.

Furthermore, if (after removing leading spaces and tabs) the first character is - or if \ occurs anywhere in the line (after combining lines ending with \), the results will vary from system to system (since echo handles options and arguments containing \ differently on different systems).
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 09-27-2013
You could try something like this as well if you don't mind the trailing slash being removed:
Code:
sed 's/\\$//g' file | while read line ; do echo $line ; echo "*****************" ; done


Last edited by Subbeh; 09-27-2013 at 11:37 AM..
This User Gave Thanks to Subbeh For This Post:
# 6  
Old 09-27-2013
Thanks Don...
# 7  
Old 09-27-2013
Quote:
Originally Posted by Subbeh
You could try something like this as well if you don't mind the trailing slash being removed:
Code:
sed 's/\\$//g' file | while read line ; do echo $line ; echo "*****************" ; done

And if you don't mind losing slashes embedded in the line, as well as leading and trailing whitespace. Refer to post #4.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with while loop reading every line of a text file

Hello, I'm using RHEL 5.1 with bash. How to handle "read" inside while loop reading every line? Please see below: # cat /tmp/passwd_sample CARRJ12:qVSn4ja4mFA72,..:20021:125:JULIAN CARR:/home/everyone:/bin/bash HERCOT01:NK/3j2ZB4ZC7Q:20022:125:TOM HERCOCK:/home/everyone:/bin/bash... (4 Replies)
Discussion started by: reddyr
4 Replies

2. Shell Programming and Scripting

Reading line by line from live log file using while loop and considering only those lines start from

Hi, I want to read a live log file line by line and considering those line which start from time stamp; Below code I am using, which read line but throws an exception when comparing line that does not contain error code tail -F /logs/COMMON-ERROR.log | while read myline; do... (2 Replies)
Discussion started by: ketanraut
2 Replies

3. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

4. Shell Programming and Scripting

While loop is not reading next line in the file when IF condition is used.

Hi Guys I am new to scripting.Please forgive for asking basic questions. I want to write a script to check whether the logs are getting updated in last 15 mins. cat server 192.168.1.6 192.168.1.7 cat list 192.168.1.7 /logs/logpath1 192.168.1.7 /logs/logpath2 192.168.1.6... (4 Replies)
Discussion started by: vdurai
4 Replies

5. Shell Programming and Scripting

Read file using while loop not reading last line

I have written a script to read the file line by line. It is reading and printing the lines. But it is coming out of loop before reading last line. So I am not able to print last line. How do I solve it. (6 Replies)
Discussion started by: dgmm
6 Replies

6. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

7. Shell Programming and Scripting

Reading a file line by line and processing for each line

Hi, I am a beginner in shell scripting. I have written the following script, which is supposed to process the while loop for each line in the sid_home.txt file. But I'm getting the 'end of file' unexpected for the last line. The file sid_home.txt gets generated as expected, but the script... (6 Replies)
Discussion started by: sagarparadkar
6 Replies

8. Shell Programming and Scripting

while loop not reading last line

hi all, i have a while loop which i am using to read lines into an array: k=0 exec 10<file while read LINE <&10; do ARRAY=$LINE ((k++)) done exec 10>&- echo ${ARRAY} for some reason when i display the array it is not showing the last row in the file. any help appreciated. (1 Reply)
Discussion started by: npatwardhan
1 Replies

9. Shell Programming and Scripting

Reading from a specific line in a loop

Hello All, Request you to let me know how to do the below urgently.. Requirement File A Contains: for i in file A DEV1 DEV5 STG1 STG5 File B Contains: for j in file B DEV1 DEV5 STG1 STG5 (3 Replies)
Discussion started by: kaushikraman
3 Replies
Login or Register to Ask a Question