grep commands

gudii9 used Ask the Experts™
grep "testing" 789.log

zgrep "testing"  123.gz

i like to redirect above production server output to
abc/def/test.txt file which is on some other system test server where i have access

how to do it
grep "testing" 789.log >> abc/def/test.txt
above gives error no such directory as that directory is not there in production server

grep "testing" 789.log
how above different from
grep -C2 "testing" 789.log

i see below time stamp lines came 3 of them when i use -C2
2019-Jan-17 04:50:51.198 EST

i see below time stamp lines came 3 of them when i did not use -C2
2019-Jan-17 04:50:51.198 EST

please advise
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
grep "testing" 789.log >> abc/def/test.txt will not work if the directories abc or def do not already exist.  If they exist then it will create test.txt

I don't really understand what you're asking in the 2nd part.
David FavorFractional CTO
Distinguished Expert 2018

Maybe post .txt files with your data also, as having actual data is far easier to test then eyeballing images.
C2 means print 2 lines above and below each matched line. read the man page.


redirecting the grep to a different host can be done in many ways. if you have ssh access between hosts, that could be quite simple.

for example, run this on the development server

ssh production_server grep "testing" 789.log >> abc/def/test.txt

which will run the grep command on the prod server but the redirection occurs on the dev server

or the other way round : run on the prod server

grep "testing" 789.log | ssh dev_server tee -a abc/def/test.txt > /dev/null

many other ways are available : using netcat on both sides, using a shared drive, ...
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!


C2 means print 2 lines above and below each matched line. read the man page.

which command i have to look in man page for this?
please advise


man C2

above not working

man ls
man pwd

etc. are working fine though
man grep
really you need to figure such basics yourself
I believe you've asked about this before and got answers to the -C2, -B3, and -A4 options to grep.  You really should run the commands in a Linux environment to test them.  It's much, much faster to run them yourself to see the results, than to ask and wait around for a response.

if you NFS mount the remote system then you can redirect without any problem.


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial