I'm using find -mtime -2
to find files modified in the past 24 hours or less.
The output looks like this.
/home/user/logs/file-2014-08-22.log
/home/user/logs/file-2014-08-23.log
I need to save the first line of the output to a variable and then save the second line to a separate variable. I can't just pipe it. I know you might suggest | grep 22
or 23
but this is part of a bash script that will run many times, there will be a different set of files with different names next time so grep
would be too specific. Could awk
accomplish this? If so, how? .
Best Answer
Assuming that there are no spaces or the like in any of your filenames, there are a couple of ways of doing this. One is just to use an array:
files
will be an array of all the paths output byfind
in order, indexed from zero. You can get whichever lines you want out of that. Here I've saved the second and third lines intoa
andb
, but you could use the array elements directly too.An alternative if you have GNU find or another with the
printf
option is to use it in combination withread
and process substitution:This one turns all of
find
's output into a single line and then provides that line as the input toread
, which saves the first word (path) intojunk,
the second intoa
, the third intob
, and the rest of the line intojunk
again.Similarly, you can introduce the
paste
command for the same effect on any POSIX-compatible system:paste -s
will convert its input into a single tab-separated line, whichread
can deal with again.In the general case, if you're happy to execute the main command more than once (not necessary here), you can use
sed
easily:That will print only the second line of the output, by suppressing ordinary output with
-n
and selecting line 2 top
rint. You can also stitch togetherhead
andtail
for the same effect, which will likely be more efficient in a very long file.All of the above have the same effect of storing the second and third lines into
a
andb
, and all still have the assumption that there are no spaces, tabs, newlines, or any other characters that happen to be in your input field separator (IFS
) value in any of the filenames.Note though that the output order of
find
is undefined, so "second file" isn't really a useful identifier unless you're organising them to be ordered some other way. It's likely to be something close to creation order in many cases, but not all.