How to read last line of a txt file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to read last line of a txt file?
# 1  
Old 06-13-2005
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.

So.. instead of using a bunch of greps and cuts and then expr comparisons to find the last file for that day based on date/time.
I'm just assuming that the last line of each myLs.txt will be the last file for that day.

Does awk have a "last line" variable?
I was looking for it and I only found ones for number of "fields per record" and "current record number", but no "number of records total".

Hmm... I guess it would be better if i just, wc -l the textfile and then redirect that into a variable which I will then use in my awk command to echo the last line..... That would work wouldn't it.. hmm..

Last edited by yongho; 06-13-2005 at 01:45 PM..
# 2  
Old 06-13-2005
hrmm

firstVar=`wc -l < ls.txt` # get wc -l
firstVar=`echo $firstVar| tr -d ' '` # remove extra spaces
echo "firstVar:${firstVar}" # echo test to see if var worked
secondVar=`awk "END {print $$firstVar}" ls.txt` # awk to print last line of file

Hm... I think I'm complicating things.
I'm getting closer.

ah hah! There's a tail command.

Last edited by yongho; 06-13-2005 at 02:20 PM..
# 3  
Old 06-13-2005
Quote:
Originally Posted by yongho
firstVar=`wc -l < ls.txt` # get wc -l
firstVar=`echo $firstVar| tr -d ' '` # remove extra spaces
echo "firstVar:${firstVar}" # echo test to see if var worked
secondVar=`awk "END {print $$firstVar}" ls.txt` # awk to print last line of file

Hm... I think I'm complicating things.
# print the last line of a file (emulates "tail -1")
sed '$!d' # method 1
sed -n '$p' # method 2

# print first line of file (emulates "head -1")
sed q

sed1liners
 
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. Shell Programming and Scripting

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? (5 Replies)
Discussion started by: _-_shadow_-_
5 Replies

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

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

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