在使用 java(Jsch) 进行一些 grep 操作后,我如何从 unix 服务器读取文件

How i can read file from unix server after doing some grep operation using java(Jsch)

我在服务器上有一个大小为 200 的文本文件 mb.I 想使用命令 (grep keyword1 filename.txt|grep -v keyword2) 减少文件内容,然后在 java 使用 Jsch 连接。

我通过以下代码解决了这个问题:-

JSch jsch = new JSch();
Session session = null;
try {
  String command="grep 'keyword1' filename.txt|grep -v 'keyword2'";
  session = jsch.getSession(userid, servername, serverport);
  session.setPassword(password);
  session.setConfig("StrictHostKeyChecking", "no");
  session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
  session.connect();

  Channel channel = session.openChannel("exec");
  ((ChannelExec)channel).setCommand(command);
  channel.connect();

  InputStream commandOutput = channel.getInputStream();
  BufferedReader br = new BufferedReader(new      InputStreamReader(commandOutput));    
  String line="";                           
  while ((line = br.readLine()) != null)
 {
        System.out.println(line);                               
 }
  br.close();
  channel.disconnect();
  session.disconnect();
} catch (Exception e) {
  e.printStackTrace();
}