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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Grep multiple files in reverse order.

Status
Not open for further replies.

hg4372

Technical User
Feb 9, 2009
43
US
I'm running the following on multiple files in a directory, and. It works fine, however I would like the most recent file be at the top and the oldest at the bottom. Is this possible ?

grep "Test" /export/home/test/market1* > /export/home/test_history/market1.HIS.txt

/export/home/test/market1.test.0602101616 Test Passed
/export/home/test/market1.test.0602101621 Test Passed
/export/home/test/market1.test.0602101626 Test Passed
/export/home/test/market1.test.0602101631 Test Passed

Can I grep the files in reverse order so my history file would look like this ?

/export/home/test/market1.test.0602101631 Test Passed
/export/home/test/market1.test.0602101626 Test Passed
/export/home/test/market1.test.0602101621 Test Passed
/export/home/test/market1.test.0602101616 Test Passed
 
PErhaps something like this ?
ls -tr /export/home/test/market1* | xargs grep "Test"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks for the quick reply. Looks like that will work.

Thanks again for your help!
 
I changed my KSH script to add this line, and it still has them in order of the oldest to newest.
#!/usr/bin/ksh
# echo "Enter switch: \c"
# read switch
# echo "Enter username: \c"
# read username
# echo "Enter password: \c"
# read password
market=$1
dt=$(date +%m%d%y_%H%M)
username=username
password=password

for x in 1
do
{
sleep 5
echo $username
sleep 3
echo $password
sleep 3
echo "print test"
sleep 3
echo "quit all;logout"

}|telnet $switch>/export/public1/index_files/$market.txt

cp /export/public1/index_files/$market.txt /export/home/test/$market_$dt

done

ls -ltr /export/home/test/market1* | xargs grep "Test" > /export/home/test/history/market1.HIS.txt

The question is. Does the above ls -ltr command have to be entered differently while in KSH. It doesn't seem to put the grep in reverse order.

It seemed to work when I typed it manually though.
 
Another way:
grep "Test" /export/home/test/market1* | sort -r /export/home/test/history/market1.HIS.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
OOps,: sorry for the typo
grep "Test" /export/home/test/market1* | sort -r >/export/home/test/history/market1.HIS.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Excellent. Thank you. That worked.

Thank you soooooo much.

hg4372
 
Code:
ls -[red]l[/red]tr /export/home/test/market1* | xargs grep "Test" > /export/home/test/history/market1.HIS.txt

This will probably result in a bunch of errors too with the -l option, because grep will try to open files called -rwxr-xr-x and similar, i.e. all of the output of ls -l, not just the filename.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top