I have a multiples files in a remote server. I got grep/sed/awk the files but I have not been able to obtain context lines, before and after the match for each file and then append in one file.
With sed I have achieved from pattern to the end of file got the lines but no before pattern. With awk I got the line number of the match for each file but got some errors with find and -exec. I am beginner in Linux and regex. What am I doing wrong?
First attempt:
sshpass -p password ssh user@server "find /data/ -name "*.txt" -type f -exec ksh -c "grep -n $KEY $1 | cut -d':' -f1 | xargs -n1 -I% awk 'NR<=%+5 && NR>=%-5' $1 " ksh {} \; -print" > output.txt
It seems works fine until xargs command. I got this error: find: 0652-018 An expression term lacks a required parameter
.
Second attempt:
sshpass -p password ssh user@server "find /data/ -name "*.txt" -type f -exec grep -n $KEY {} \; | cut -d':' -f1 | xargs -n1 -I% -exec awk 'NR<=%+5 && NR>=%-5' {} \; -print" > output.txt
I got:
awk: 0602-533 Cannot find or open file {}. The source line number is 1. awk: 0602-533 Cannot find or open file {}. The source line number is 1. awk: 0602-533 Cannot find or open file {}. The source line number is 1.
Third attempt:
sshpass -p password ssh user@server "find /data/ -name "*.txt" -type f -exec sed -n '/$KEY/,$ p' {} \;" > output.txt
With sed seems works fine with simple words and I can obtain lines from patterns to the end of each file. But I can't get expressions like this "word1.*word2" (words in same line) works.
$KEY
is my variable with the pattern to match.