Reputation: 369
I am trying to use scp to copy large log files from a remote server. However I want only the lines in remote log file that has a string "Fail". This is how I am doing it currently
scp user@ip:remote_folder/logfile* /localfolder
This copies all the files starting with logfile in remote server to my local folder. The files are pretty large and I need to copy only the lines in those log file, containing the string "Fail" from remote server. Can any body tell me how to do this? Can I use cat or grep command?
Upvotes: 1
Views: 2532
Reputation: 3981
Use grep
on the remote machine and filter the output into file name and content:
#!/usr/bin/env bash
BASEDIR=~/temp/log
IFS=$'\n'
for match in `ssh user@ip grep -r Fail "remote_folder/logfile*"`
do
IFS=: read file line <<< $match
mkdir -p `dirname $BASEDIR/$file`
echo $line >> $BASEDIR/$file
done
You might want to look at an explanation to IFS
in combination with read
.
Upvotes: 2