How to get cat to process a file name provided in the output of another command

I wanted to pipe the output of the find command through the tail command and then pipe its outout into the cat command. E.g., I used the find command to locate the manual page for the curl command on a MacBook Pro running OS X as shown below:
$ find /usr/share/man -name curl\*
/usr/share/man/man1/curl-config.1
/usr/share/man/man1/curl.1
$

There were two man pages with "curl" as part of the file name, but I only wanted the second one, so I piped the output of find into tail, selecting the last line of output only with the -n 1 option.

$ find /usr/share/man -name curl\* | tail -n 1
/usr/share/man/man1/curl.1
$

I then wanted to have cat process that file name. I could have just typed the directory path and file name produced from the above sequence of commands or copied and pasted the result, of course, but I thought it would be useful to know a method to get cat to process the output from find for other situations. There is a simple method, using command substitution of getting cat to process a file name that find has located. One can simply use a command similar to cat `find [whatever]` as explained by Laurence Gonsalves in response to a Stack Overflow question How to pipe list of files returned by find command to cat to view all the files. The command subsitution takes the output of the command or commands between successive backtick characters and uses that as the argument for another command, in this case the cat command.

So, in this case, I could use the command cat `find /usr/share/man -name curl\* | tail -n 1`, which pipes the output of find through tail with the output of that command serving as the input filename to the cat command. Since, I wanted to then feed the output of cat into the groff command to convert the curl man page to HTML, as explained at Converting a man page to HTML, PDF, text, I could then construct the final command below to get the HTML version of the man page.

1&1 Web Hosting
$ cat `find /usr/share/man -name curl\* | tail -n 1` | groff -mandoc -Thtml >curl.html
stdin:275: warning [p 1, 49.2i]: cannot adjust line
stdin:275: warning [p 1, 49.3i]: can't break line
$

Alternatively, I can use another variant of the command substitution technique within the Bash shell to achieve the same effect by enclosing the command(s) I want to use to produce output that will serve as the input to another command, in this case the cat command, within the parentheses of $() as noted by Stphane in response to the same question and as shown below:

$ cat $(find /usr/share/man -name curl\* | tail -n 1) | groff -mandoc -Thtml >curl.html
stdin:275: warning [p 1, 49.2i]: cannot adjust line
stdin:275: warning [p 1, 49.3i]: can't break line
$

Note: the commands above, though run from a Bash shell prompt obtained via the Terminal application on an Apple OS X system, will also work from a shell prompt on a Linux or Unix system as well.

 

TechRabbit ad 300x250 newegg.com

Justdeals Daily Electronics Deals1x1 px