Bash – Saving individual output lines into variables

bashoutputshell

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=( $(find -mtime -2) )
a=${files[1]}
b=${files[2]}

files will be an array of all the paths output by find in order, indexed from zero. You can get whichever lines you want out of that. Here I've saved the second and third lines into a and b, 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 with read and process substitution:

read junk a b junk < <(find -printf '%p ')

This one turns all of find's output into a single line and then provides that line as the input to read, which saves the first word (path) into junk, the second into a, the third into b, and the rest of the line into junk again.

Similarly, you can introduce the paste command for the same effect on any POSIX-compatible system:

read junk a b junk < <(find -mtime -2 | paste -s)

paste -s will convert its input into a single tab-separated line, which read 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:

find | sed -n 2p

That will print only the second line of the output, by suppressing ordinary output with -n and selecting line 2 to print. You can also stitch together head and tail 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 and b, 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.

Related Question