Sponsored Content
Top Forums Shell Programming and Scripting How do i read only last 5 files records from a directory.? Post 303006057 by sadique.manzar on Thursday 26th of October 2017 05:17:47 PM
Old 10-26-2017
The directory keep geeting files continously.
So if i do ls -l | tail -5
I get that latest 5 files,
My main concern is i need to read each files in loop
Then awk few data from the files comeout and do some calculation.

For all the files inside a directory it os working, but i want to implement it only last 5 files of the directory.

---------- Post updated at 04:17 PM ---------- Previous update was at 09:29 AM ----------

here is my code:
Code:
#!/bin/sh
DATE=`date +"%d-%m-%Y-%H:%M"`
stream=IUCS
FLAG=LAST
path=/log/INVESTIG/TEST
for  files in $path/*
        do
        for f in $files
        do
        #echo $files
        read -r line || [ -n "$line"];
        TTHEX=`awk -F ',' 'END{print $4}'`
                done < $files
                TIMESTAMP=$( date +'%H:%M:%S' -r $files)
                TRANS_TIME=$(date -d @$(expr `printf "%d" 0x$TTHEX` / 1000) | awk '{print $4}')
                TIME_LAG=$(date +%H:%M:%S -ud @$((`expr $(date -u -d "$TIMESTAMP" +"%s") - $(date -u -d "$TRANS_TIME" +"%s")`)))
 
        echo "${DATE} ${stream} ${FLAG} $(ls -l $files | awk '{print $9}'| cut -d '/' -f5) ${TIMESTAMP} ${TRANS_TIME} ${TIME_LAG}"
        done



Output:
Code:
26-10-2017-15:24 IUCS LAST TDR-IU-8-139 11:12:23 10:14:59 00:57:24
26-10-2017-15:24 IUCS LAST TDR-IU-8-127 15:22:33	15:14:59 00:07:34
26-10-2017-15:24 IUCS LAST TDR-IU-8-140 15:22:33 15:14:59 00:07:34
26-10-2017-15:24 IUCS LAST TDR-IU-8-59   15:22:33 15:14:59 00:07:34

My script is reading all the files of the directory.
for files in $path/*

instead of it i want to read only last 5 files of the directory

kindly help.

Last edited by MadeInGermany; 10-26-2017 at 07:07 PM.. Reason: added code tags
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read from fileList.txt, copy files from directory tree

Hi, hopefully this is a fairly simple Q&A. I have a clean file list of approximately 180 filenames with no directory or slashes in front of the filename nor any extension or dot ".". I would like to read from this list, find these files recursively down through directory trees, copy the files... (1 Reply)
Discussion started by: fxvisions
1 Replies

2. UNIX for Dummies Questions & Answers

Read Files from a Directory

Hi, I have a requirement where I have get the files from FTP server then delete the files in the FTP server and then store the file name within the file where there is "TR". i.e Filename|TR|20071231|.... Once I finish loading my file I have to archive the files. Is there any way to do it... (2 Replies)
Discussion started by: kiran_418
2 Replies

3. Shell Programming and Scripting

read files from directory

hi i have a directory /tmp/Satya,it contains 5.FILE 6.FILE 7.FILE i need to read each file , and read its content line by line please help thanks Satya (2 Replies)
Discussion started by: Satyak
2 Replies

4. Shell Programming and Scripting

how to read next records

Hello friends, I am newbie in programing. I am facing some problems in awk. Please help me. I have a file with many data sets. Each data set is separated by an empty line. For example Col1 Col2 Col3 Col4 Col5 0.85 0.07 Fre 42:86 25 0.73 0.03 frp 21:10 28 0.64 0.04 Fre 42:86 63 0.47 0.08... (2 Replies)
Discussion started by: ubeejani
2 Replies

5. UNIX for Dummies Questions & Answers

Read directory files and count number of lines

Hello, I'm trying to create a BASH file that can read all the files in my working directory and tell me how many words and lines are in that file. I wrote the following code: FILES="*" for f in "$FILES" do echo -e `wc -l -w $f` done My issue is that my file is outputting in one... (4 Replies)
Discussion started by: jl487
4 Replies

6. Shell Programming and Scripting

How to read and append certain files form directory

Hi ,i have a question ,if I am in sertain directory and I have 4 files *.c how can I read and append this files in file backup.bac Thanks find ./ -name "*.csh" | wc -l (2 Replies)
Discussion started by: lio123
2 Replies

7. UNIX for Dummies Questions & Answers

Read all files in a directory for a unix command

Hello, Below, I have a unix command, which can be executable for single file. cat input.txt | sort -k3,3 > output.txt I have 100 input files in a directory. It is hectic and time taking to run the above command for all the 100 files for 100 times. Now, I want to execute the above unix... (2 Replies)
Discussion started by: koneru_18
2 Replies

8. Shell Programming and Scripting

Compare two files with different number of records and output only the Extra records from file1

Hi Freinds , I have 2 files . File 1 |nag|HYd|1|Che |esw|Gun|2|hyd |pra|bhe|3|hyd |omu|hei|4|bnsj |uer|oeri|5|uery File 2 |nag|HYd|1|Che |esw|Gun|2|hyd |uer|oi|3|uery output : (9 Replies)
Discussion started by: i150371485
9 Replies

9. Shell Programming and Scripting

How to read a particular records from a file?

Hi All Can anybody let me know the code to read a particular record from a file For e.g. File Name: File.txt File content: Script_path=/abc/def/script/ File_path=/xyz/data/ Business Date=19990905 SERVER_NAME=Server DATABASE_NAME=Database Login=NewUser Password=NewPassword ... (3 Replies)
Discussion started by: Siddhartha9833
3 Replies

10. Shell Programming and Scripting

How to fetch matched records from files between two different directory?

awk 'NR==FNR{arr;next} $0 in arr' /tmp/Data_mismatch.sh /prd/HK/ACCTCARD_20160115.txt edit by bakunin: seems that one CODE-tag got lost somewhere. i corrected that, but please check your posts more carefully. Thank you. (5 Replies)
Discussion started by: suresh_target
5 Replies
IGAWK(1)							 Utility Commands							  IGAWK(1)

NAME
igawk - gawk with include files SYNOPSIS
igawk [ all gawk options ] -f program-file [ -- ] file ... igawk [ all gawk options ] [ -- ] program-text file ... DESCRIPTION
Igawk is a simple shell script that adds the ability to have ``include files'' to gawk(1). AWK programs for igawk are the same as for gawk, except that, in addition, you may have lines like @include getopt.awk in your program to include the file getopt.awk from either the current directory or one of the other directories in the search path. OPTIONS
See gawk(1) for a full description of the AWK language and the options that gawk supports. EXAMPLES
cat << EOF > test.awk @include getopt.awk BEGIN { while (getopt(ARGC, ARGV, "am:q") != -1) ... } EOF igawk -f test.awk SEE ALSO
gawk(1) Effective AWK Programming, Edition 1.0, published by the Free Software Foundation, 1995. AUTHOR
Arnold Robbins (arnold@skeeve.com). Free Software Foundation Nov 3 1999 IGAWK(1)
All times are GMT -4. The time now is 06:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy