grep commands

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 FavorLinux/LXD/WordPress/Hosting SavantCommented:
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, ...
Get a highly available system for cyber protection

The Acronis SDI Appliance is a new plug-n-play solution with pre-configured Acronis Software-Defined Infrastructure software that gives service providers and enterprises ready access to a fault-tolerant system, which combines universal storage and high-performance virtualization.

gudii9Author Commented:
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
gudii9Author Commented:
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.