Sam Rohn
Sam Rohn

Reputation: 369

scp: how to copy a file from remote server with a filter

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

Answers (2)

Finwood
Finwood

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

dmi
dmi

Reputation: 1479

ssh user@ip grep Fail remote_folder/logfile*

Upvotes: 2

Related Questions