how to determine last line in a while do loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to determine last line in a while do loop
# 1  
Old 06-19-2007
how to determine last line in a while do loop

Hey guy, I have a very simple question, I have the following script,

while read $test
do
echo "this is $test"
done < /testfile

What I want is instead of printing "this is $test" for each line, I want the script to print out "this is last line" for last line, how should I do it?

Last edited by fedora; 06-19-2007 at 03:41 PM..
# 2  
Old 06-19-2007
You won't know it's the last line until you fall out of the loop

do something like

Code:
LINE=

while read N
do
      if test "$LINE" != ""; then echo this is $LINE; fi
      LINE="$N"
done <file

echo this is last line $LINE

# 3  
Old 06-19-2007
Fedora,
If you just want to display the last line, here is one way:
Code:
tail -1 input_file

# 4  
Old 06-19-2007
thanks guys, i think I did not explain clearly of my question,

I have a bunch of files to process, which contain many usernames, I want every last name in every file to be treated special, something like this (I know this is not a real shell scipt):

while read name

do

if "the position of name"!="last name in the file"; then

function1

else

function2

fi

done < /tmp/filename/*
# 5  
Old 06-19-2007
Here is one way:
Code:
typeset -i mTotLines
typeset -i mCntLines=0
mTotLines=`cat input_file | wc -l`
while read mLine
do
  mCntLines=${mCntLines}+1
  if [ ${mCntLines} -eq ${mTotLines} ]; then
    echo "Last line"
  else
    echo "Not the last line"
  fi
done < input_file

# 6  
Old 06-19-2007
Quote:
Originally Posted by fedora
thanks guys, i think I did not explain clearly of my question,

I have a bunch of files to process, which contain many usernames, I want every last name in every file to be treated special, something like this (I know this is not a real shell scipt):
Code:
while read name
do 
  if "the position of name"!="last name in the file"; then
    function1
  else
    function2
  fi
done < /tmp/filename/*


Code:
prev=
while IFS= read -r line
do
   [ -n "$prev" ] && function1 "$prev"
   prev=$line
done < FILE
function2 "$prev"

# 7  
Old 06-19-2007
ah, never thought this, thank you Shell_Life!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Another one line command where I'd like to determine if Ubuntu or Red Hat when running command

Hello Forum, I'm making very good progress on my report thanks to the very helpful people on this forum. I've been able to successfully create my report for my Red Hat servers. But I do have a few ubuntu servers in the mix and I'd like to capture some data from them when an ssh connection is... (8 Replies)
Discussion started by: greavette
8 Replies

2. Shell Programming and Scripting

Getting an unexpected newline in my while loop line-by-line feed

Hi, I'm trying to get a line returned as is from the below input.csv file in Bash in Linux, and somehow I get an unexpected newline in the middle of my input. Here's a sample line in input.csv $> more input.csv TEST_SYSTEM,DUMMY@GMAIL.COM|JULIA H|BROWN And here's a very basic while loop... (7 Replies)
Discussion started by: ChicagoBlues
7 Replies

3. Shell Programming and Scripting

While read line loop

Based on text file: PATH:/media/hdd/Media/Video/title1 FILE:/media/cache/281662-14.jpg PATH:/media/hdd/Media/Video/title2 FILE:/media/cache/281662-15.jpg PATH:/media/hdd/Media/Video/title3 FILE:/media/cache/281662-16.jpg PATH:/media/hdd/Media/Video/title4 FILE:/media/cache/281662-17.jpg... (12 Replies)
Discussion started by: TiedCone
12 Replies

4. 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

5. Shell Programming and Scripting

for loop with whole line using cat

Hi all, I need to create loop script to read full line and append a variable to each line. cat file I need the output like below 10.0.0.1,136 1 24 048800 id N4 No_Light 10.0.0.1,137 1 25 048900 id N4 No_Light 10.0.0.1,140 1 28 048c00 id N4 No_Light 10.0.0.1,262 1 38 048e80... (13 Replies)
Discussion started by: ranjancom2000
13 Replies

6. UNIX for Dummies Questions & Answers

One Line for loop in CRON

Hello, I'm trying to run a one line for loop from the CRON. This is the exact command /bin/bash for file in $(/bin/find /opt/local/edw_extract/logs -name "*.log"); do /bin/cat /dev/null > $file; done This will run from the command successfully. When I schedule this to run it will... (3 Replies)
Discussion started by: mhauff
3 Replies

7. Shell Programming and Scripting

While read line loop

Hi I'm writing a bash script which will read an input file and look for occurrences of the current user ($USER) executing the script. When i find the occurrence of the username I take that line and append it to a file with a line number and bracket display next to line. The input file has been... (12 Replies)
Discussion started by: BundBash
12 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

read line in a for loop

Hi All, How can we use read line using the index value of a FOR loop? eg: pt_mstr,pt_status,8 pt_mstr,pt_buyer,8 pt_mstr,pt_sfty_stk,8 pt_mstr,pt_ord_pol,3 pt_mstr,pt_capacity,8 pt_mstr,pt_plan_ord,3 pt_mstr,pt_ord_mult,8 From this file i want to read the line2, 3 and 4 only using a FOR... (3 Replies)
Discussion started by: balajim
3 Replies

10. UNIX for Dummies Questions & Answers

determine if crontable line should have been executed

Hi there, Is there a command in UNIX that could determine if the line in a crontable should be executed or not? Just as if the command is the crontab command itself. Say I have this crontable file: 10 10 * * * /usr/bin/script1.sh 20 11 * * * /usr/bin/script2.sh 30 12 * * 2... (1 Reply)
Discussion started by: Orbix
1 Replies
Login or Register to Ask a Question