i need to read the last line in a txt file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting i need to read the last line in a txt file
# 1  
Old 02-27-2009
i need to read the last line in a txt file

i'm a beginner in shell and i have a txt file that is updating every second or msec so i need a program to read the last line of this txt file
is this possible to do?
# 2  
Old 02-27-2009
Hammer & Screwdriver

Code:
tail -1 myfile

will output the last line in myfile
# 3  
Old 02-27-2009
Alternately, if u want to see the file updates while excuting the tail add -f to the tail command. Like

#tail -f <file name>
# 4  
Old 02-27-2009
extra question

thanks,

i forget to say after checking the last line the number in the last line i want to use it, for example let's say that the txt file contains:

1
2
1
4
6
1

so the last line contains 1 and i want to use this number
# 5  
Old 02-27-2009
Hammer & Screwdriver

Code:
myval=`tail -1 myfile`

or
Code:
myval=$(tail -1 myfile)

in some flavors of unix
# 6  
Old 02-27-2009
Quote:
Originally Posted by joeyg
Code:
myval=`tail -1 myfile`

or
Code:
myval=$(tail -1 myfile)

in some flavors of unix

The POSIX spec for tail uses the syntax:

Code:
tail -n1 FILE

Apart from a brief time when the GNU version only accepted the POSIX syntax, the older syntax will work in all versions.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read in txt file and run a different command for each line

hi, i'm trying to write a tcsh script that reads in a text file (one column) and the runs a different command for each line of text. i've found lots of example commands for bash, but not for tcsh. can anyone give me a hint? thanks, jill (8 Replies)
Discussion started by: giuinha
8 Replies

2. UNIX for Dummies Questions & Answers

Split Every Line In Txt Into Separate Txt File, Named Same As The Line

Hi All Is there a way to export every line into new txt file where by the title of each txt output are same as the line ? I have this txt files containing names: Kandra Vanhooser Rhona Menefee Reynaldo Hutt Houston Rafferty Charmaine Lord Albertine Poucher Juana Maes Mitch Lobel... (2 Replies)
Discussion started by: Nexeu
2 Replies

3. Shell Programming and Scripting

Need to append the date | abcddate.txt to the first line of my txt file

I want to add/append the info in the following format to my.txt file. 20130702|abcd20130702.txt FN|SN|DOB I tried the below script but it throws me some exceptions. <#!/bin/sh dt = date '+%y%m%d'members; echo $dt+|+members+$dt; /usr/bin/awk -f BEGIN { FS="|"; OFS="|"; } { print... (6 Replies)
Discussion started by: harik1982
6 Replies

4. Shell Programming and Scripting

Want to read data from a file name.txt and search it in another file and then matching...

Hi Frnds... I have an input file name.txt and another file named as source.. name.txt is having only one column and source is having around 25 columns...i need to read from name.txt line by line and search it in source file and then save the result in results file.. I have a rough idea about the... (15 Replies)
Discussion started by: ektubbe
15 Replies

5. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

6. Shell Programming and Scripting

How to read from txt file and write it to xls?

Hello All, I just want help in coding a simple shell script since i am a newbie for UNIX and i started learning unix and shell scripting basics recently. I am having a data like this in .txt file. Product Name : XYZ Price : 678.1 Best Buy Price : 600 Product Name : ABC Price : 465... (3 Replies)
Discussion started by: vasanth_123
3 Replies

7. UNIX for Dummies Questions & Answers

can't read a .txt file

Hello, I have a set of .txt files I cannot read. This is a part of what I see. Is there a way to view these files? _MO<P.6D@K;WU<B$X-;)SIV/ROO!UL+1P=VTT-?,SLC`MI/6QMS#UYGGT\+)C=#\UIO`TL/0]=#/T) it's about 3 pages. Thanks for your help. Joe (3 Replies)
Discussion started by: rcracerjoe
3 Replies

8. UNIX for Dummies Questions & Answers

How to read from txt file and use that as an array

Hi Guys How u all doing? I am having tough time to achieve this I have a unix .ksh script which calls sql script Right now I harcoded column id's in sql script but I want to read them from a txt file 1084,1143,1074,1080,1091,1090,1101,1069,1104,1087,1089,1081 I want to read this... (4 Replies)
Discussion started by: pinky
4 Replies

9. UNIX for Dummies Questions & Answers

How to read last line of a txt file?

I need to read the last file for a particular day, such as, "Jun 13" because the CSV file is cumulative for the entire day, so I don't want all the previous files, I just want the last file, for that day. I ran an 'ls -al | grep "June 13" > myLs.txt' (simplified) to list all files from that day.... (2 Replies)
Discussion started by: yongho
2 Replies

10. UNIX for Dummies Questions & Answers

want to read txt fileline by line

Hi , i want to read a text file line by line , is there any unix command or utility for this ? please tell me how to use it also ? (2 Replies)
Discussion started by: dharmesht
2 Replies
Login or Register to Ask a Question