Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ls then pipe what it finds to cat?

Status
Not open for further replies.
Feb 14, 2002
88
JP
Haven't played with bash in a while. I'm actually writing a Perl script and using a bash command to get my input. Instead of using some goofy subroutine to find out what the newest file is, I'm trying to do a directory listing, grep out the files I want to see (in reverse order) tail by 1 so I get the latest file, and then I want to cat out the contents.

I'm doing all of this in a RSH command, and to top it off (as stated before), that command is actually going into a filehandle in perl, so this needs to be a one-liner. I tried
rsh nodename ls -tr /path/to/dir |grep "string" |tail -1
which gives me the file. When I try
rsh nodename ls -tr /path/to/dir |grep "string" |tail -1 |cat
then I still get the file name -- it doesn't cat the contents.

Any help is appreciated.
 
Have you tried something like this ?
1) If the ls must be done remotely
rsh nodename 'cat `ls -tr /path/to/dir |grep "string" |tail -1`'
2) If the ls must be done locally
rsh nodename cat `ls -tr /path/to/dir |grep "string" |tail -1`

Hope This Help
PH.
 
Try:
rsh nodename ls -tr /path/to/dir |grep "string" |tail -1 | xargs cat


tikual
 
Thanks for the posts guys. Xargs is what I wanted.

PHV: Your solution worked except for one thing - it tried to cat $HOME/file -- not /path/to/dir/file. In this case, tikual's solution worked.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top