how to view last line of multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to view last line of multiple files
# 1  
Old 05-24-2009
how to view last line of multiple files

Dear All,

can anybody help me out in generating a command that can be used to view the last line of multiples files.

e.g:

file 1 contains 100 records
file 2 contains 200 records
file 3 contails 300 records

now i need a command that can be used to display the last line of each file. (i.e. file 1 file 2 & file 3)

an urgent reply in this regard will be highly appreciated.Smilie
# 2  
Old 05-24-2009
How about
Code:
$ tail file1 file2 file3

# 3  
Old 05-24-2009
Code:
#!/bin/ksh
for filename in "file 1" "file 2" "file 3"
do
         # If there is anything in the file, show which file and the last line
         if [ -s "${filename}" ]
         then
                 ls -lad "${filename}"
                 # tail hyphen one shows the last line
                 tail -1 "${filename}"
                 echo ""
         fi
done


Last edited by methyl; 05-24-2009 at 05:26 PM.. Reason: Code tags
# 4  
Old 05-24-2009
hi methyl,

thankz for the reply,

please advice that a folder contains more than 100 files and each files contains 1000 records. All files have a (.txt) extension. Now please help me out from this prespective.

Thanks in advance.
# 5  
Old 05-24-2009
Really should be a new post. Here is a variant on my previous post for any number of text files in the current directory. Hope this helps.

Code:
#!/bin/ksh
ls -1 *\.txt 2>/dev/null | while read filename
do
         # If there is anything in the file, show which file and the last line
         if [ -s "${filename}" ]
         then
                 ls -lad "${filename}"
                 # tail hyphen one shows the last line
                 tail -1 "${filename}"
                 echo ""
         fi
done


Last edited by methyl; 05-24-2009 at 05:52 PM.. Reason: Allow for no files
# 6  
Old 05-24-2009
thankz a million methyl,

now please guide me in knowing the logic for this script line by line if possible.

Thankz in advance.Smilie
# 7  
Old 05-24-2009
Hre we go. I have inserted comment lines which may help. However if you see a command you need to know more about use "man <command>".

Code:
#!/bin/ksh
# The above line forces Korn Shell. It could equally be bash.
#
# ls hyphen one lists the names of files and directories in the current directory
# We have assumed that there are no directory names ending in ".txt"
# The "2>/dev/null" redirects the error channel 2 to a black hole so
# the script does not fail if we have no files in the directory.
# The next line places the list of filenames onto a pipeline for processing.
# The "\." escapes the full stop such that we definitely get a fulll stop (this is unix not MSDOS).
ls -1 *\.txt 2>/dev/null | while read filename
do
         # If there is anything in the file, show which file and the last line
         # "if -s" tests whether the file exists and has any size
         if [ -s "${filename}" ]
         then
                 # "ls -lad" lists the file and does not go deeper down a tree
                 ls -lad "${filename}"
                 # tail hyphen one shows the last line
                 tail -1 "${filename}"
                 # Just output a blank line for ease of reading the output
                 echo ""
         fi
done


Last edited by methyl; 05-24-2009 at 06:53 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. UNIX for Dummies Questions & Answers

Insert a line into multiple files

HI All, I want to know if it is possible to print the same message but into 2 different files in the same command? Something like . .. ... echo "Text" >> file1 && file2 this is because i creating a script which i use a log but i don't want to duplicate lines of command just to... (5 Replies)
Discussion started by: lordseiya
5 Replies

3. UNIX for Dummies Questions & Answers

Want to change common line from multiple files

Hi everyone, I've a requirement to modify an existing line which is common to multiple files. I need to replace that existing line with a new line. I've almost 900 ksh files to edit in the similar fashion in the same directory. Example: Existing Line: . $HOME/.eff.env (notice the "." at the... (3 Replies)
Discussion started by: kaleem.adil
3 Replies

4. Shell Programming and Scripting

insert filename into each line of multiple files

I need to insert <filename + comma> into each line of multiple files. Any idea how to script that? Regards, Manu (5 Replies)
Discussion started by: linux.yahoo
5 Replies

5. Shell Programming and Scripting

How to View multiple Cron jobs

Hi, I ran two crontab commands using: crontab program1 crontab program2 However when I type crontab -l only the second cron job shows up, how do I see all cron jobs running and how do I edit all at the same time Thanks in Advance S:D (10 Replies)
Discussion started by: walforum
10 Replies

6. UNIX for Dummies Questions & Answers

Finding nth line across multiple files

I have several files (around 50) that have the similar format. I need to extract the 5th line from every file and output that into a text file. So far, I have been able to figure out how to do it for a single file: $ awk 'NR==5' text1.txt > results.txt OR $ sed -n '5p' text1.txt > results.txt... (6 Replies)
Discussion started by: oriqin
6 Replies

7. Shell Programming and Scripting

bash: cat multiple files together except first line?

Hopefully the title summarized what I need help with. I have multiple files that I would like to concatenate in bash. ie: cat file1 file2 file3 > bigfile except I do not want to include the first line from each file (). Any help? Thanks. (6 Replies)
Discussion started by: sanimfj
6 Replies

8. Shell Programming and Scripting

Appending same tring to multiple files in one line?

I have a string that I need to append to 3 files. Say, $ echo "Hello" I want to append this “Hello” to three files, file1, file2 and file3.The files are all in different directories and the file names have no common pattern.Can I do it in one line? If yes, how? :confused: (2 Replies)
Discussion started by: zombiezparadize
2 Replies

9. Shell Programming and Scripting

How to Eliminate first line of multiple files

hi gurus ,, I have multiple files with same file pattern..in a particular directory for ex: file20061101.trf file20061102.trf file20061103.trf Each of the file has a header as column names.. My questions is how can i eliminate the first row of each of these... (11 Replies)
Discussion started by: sish78
11 Replies

10. UNIX for Dummies Questions & Answers

Multiple view of Apsedic to ASCII.

dd conv=ascii if=100.PCN of=foo On paging the 'foo' file i get conversion of the selected file. How can I get multiple 100.PCN's into 1 'foo' file. Usually all my work related files have an extension of .PCN or 001. Upon selecting if=*.* No ouput file is generated. (2 Replies)
Discussion started by: buRst
2 Replies
Login or Register to Ask a Question