choose the newest file and use getline to read it

Having problems with a small awk script, Im trying to choose the newest of some log files and then use getline to read it. The problem is that it dosent work if I dont send it any input first to the script.

This works

echo | myprog.awk

this do not

myprog.awk

myprog.awk

BEGIN{ #find the newest file command="ls -alrt | tail -1 | cut -c59-100" command | getline logfile close(command) } { while((getline<logfile)>0){ #do the magic print $0 } }

-------------Problems Reply------------

Your problem is that while your program selects OK the logfile the block {} is to be executed for every line of the input file and you have not input file so it defaults to standard input. I don't know awk very well myself so I don't know how to change the input (if possible) from within an awk script, so I would:

#! /bin/awk -f

BEGIN{
# find the newest file
command = "ls -1rt | tail -1 "
command | getline logfile
close(command)
while((getline<logfile)>0){
getline<logfile
# do the magic
print $0
}
}

or maybe

alias myprog.awk="awk '{print $0}' `ls -1rt | tail -1`"

Again, this maybe a little dirty. We'll wait for a better answer. :-)

Never parse ls. See this for the reason.

Why do you need to use getline? Let awk do the work for you.

#!/bin/bash
# get the newest file
files=(*) newest=${f[0]}
for f in "${files[@]}"; do
if [[ $f -nt $newest ]]; then
newest=$f
fi
done

# process it with awk
awk '{
# do the magic
print $0
}' $newest

Category:awk Views:0 Time:2009-06-26
Tags: awk

Related post

  • How do I write a Windows batch script to copy the newest file from a directory? 2008-09-18

    I need to copy the newest file in a directory to a new location. So far I've found resources on the forfiles command, a date-related question here, and another related question. I'm just having a bit of trouble putting the pieces together! How do I c

  • Choosing which JS file to load does not load file 2011-06-13

    I have a mobile app using JQuery Mobile. Because of issues with blackberry I need to choose which JS files to load when the page is loaded. I do this on the html page like so: <html> <head> <!-- Code --> <script type = "text/java

  • How can I unzip the newest file in a directory in a BAT file? 2008-10-07

    I am working on a build system. The build system posts the results as a zip file in a directory. Unfortunately I have no easy way to know the name of the zip file, because it is timestamped. For the next operation, I must decompress this zip file to

  • Java - get the newest file in a directory? 2008-11-13

    Does anybody have a snippet of Java that can return the newest file in a directory (or knowledge of a library that simplifies this sort of thing)? --------------Solutions------------- This returns the last modified: public static File lastFileModifie

  • How can I get the newest file from an FTP server? 2009-02-20

    I am using Python to connect to an FTP server that contains a new list of data once every hour. I am only connecting once a day, and I only want to download the newest file in the directory. Is there a way to do this? --------------Solutions---------

  • Best way to choose a random file from a directory 2009-03-31

    What is the best way to choose a random file from a directory in Python? Edit: Here is what I am doing: import os import random import dircache dir = 'some/directory' filename = random.choice(dircache.listdir(dir)) path = os.path.join(dir, filename)

  • Best way to choose a random file from a directory in a shell script 2009-03-31

    What is the best way to choose a random file from a directory in a shell script? Here is my solution in Bash but I would be very interested for a more portable (non-GNU) version for use on Unix proper. dir='some/directory' file=`/bin/ls -1 "$dir" | s

  • Choosing a random file from a directory (with a large number of files) in Python 2010-07-14

    I have a directory with a large number of files (~1mil). I need to choose a random file from this directory. Since there are so many files, os.listdir naturally takes an eternity to finish. Is there a way I can circumvent this problem? Maybe somehow

  • vbscript to grab newest file on ftp site 2010-09-13

    I need to comeup with a vbscript to grab a file from an ftp site where the file name is in the form "vendor(date)(date)(random#).zip" . These files are updated daily so I need a regex or way to select the newest file on the server and download it. I

  • Copy the three newest files under one directory (recursively) to another specified directory 2010-10-21

    I'm using bash. Suppose I have a log file directory /var/myprogram/logs/. Under this directory I have many sub-directories and sub-sub-directories that include different types of log files from my program. I'd like to find the three newest files (mod

  • How do I move the oldest or newest file in a directory to another with Mac OS X Terminal 2011-01-07

    I'm trying to us the following command on Mac OSX 10.6 Terminal but it does not work..Any idea what I may be doing wrong? These work... List most recent file: ls -l -t | head -2 List oldest file: $ ls -gt | tail -1 But when I try to Move the oldest f

  • Scp the Three Newest Files Using Bash 2011-04-02

    I'm trying to scp the three newest files in a directory. Right now I use ls -t | head -3 to find out their names and just write them out in the scp command, but this becomes arduous. I tried using ls -t | head -3 | scp *username*@*address*:*path* but

  • Choosing a suitable file extension for an application 2011-05-26

    I'm working on an application that runs on both Windows and Mac. The application allows users to save their work to a file and later load it back into the application - much like Word, Excel etc. What issues should I consider when choosing an appropr

  • Copy newest file with filename filter in Ant 2011-05-27

    I'm trying to copy the newest file from a directory using Ant, ideally I'd like to have a filter on the filename too (eg: '*file.java'), but I don't have any idea of how to do this I've included my script so far, but it's breaking in the JavaScript a

  • List newest file, by type (.txt), after searching recursively, in a terminal 2011-06-10

    I'm trying to get my terminal to return the latest .txt file, with path intact. I've been researching ls, grep, find, and tail, using the '|' functionality of passing results from one utility to the next. The end result would be to have a working pat

  • Rename the Newest File's Extention 2011-08-08

    I'm trying to write a command script (.cmd file) that will find the newest file and rename its extension. So if there is: file1.txt file2.txt file3.txt And file3.txt is the newest, the script should change the extension to .xml, so the end result wil

  • Git merge conflict to always take the newest file 2011-09-30

    How can I make a git conflict ALWAYS get resolved by taking the newest file (latest changed timestamp), completley without a prompt? I am building a syncscript with git backend, and I never want to actually merge a file, just override all older copie

  • Why do you think the Java language chooses to treat file data as a stream rather than as a single object? 2011-12-16

    Why do you think the Java language chooses to treat file data as a stream rather than as a single object? What are the benefits of this approach? --------------Solutions------------- Consider loading a 4 GB file into your application as an object....

  • WHAT HAPPENED TO THE FUNCTION (and the botton) OF THE "ATTACH FILE" BUTTON THAT ALLOWED US TO CHOOSE A SMALLER FILE SIZE? 2012-05-18

    I have been attaching multiple photos by clicking on "attach file" which, up to now, has been embedded right where we type our email messages. This enabled me to choose to reduce the file size of the photos. It also enabled me to send them as a slide

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.153 (s). 11 q(s)