getting the line in shell script

纵然是瞬间 提交于 2019-12-11 14:59:39

问题


Im trying to get all " RESP:0,sucess;" in line- Enter command:RESP:0,sucess;- here is my codes.how will I take it?

**for i in {2..4}
do 
   date  
   CMDCount="CMD"$i
   eval CMD="$"$CMDCount
   echo "server CMD "${CMD}""${CMDCount}"";
   echo "" 
   (sleep 1;echo $login;
    sleep 1;echo ${CMD};
    sleep 1;echo $logout;sleep 1)|telnet localhost 9685
    sleep 1

done**

the output is like these:

server CMD xxxxxxxxxxxxxxxxxxxxxxxxxxxxCMD2

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
CONNECTING TO xxx...
PROCESS CPL CONNECTED...
Enter command:RESP:0,sucess;
Enter command:
Enter command: 
Enter command:Connection closed by foreign host.
Sat Aug  3 12:18:21 CST 2013
server CMD xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxCMD3

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
CONNECTING TO xxx...
PROCESS CPL CONNECTED...
Enter command:RESP:0,sucess;
Enter command:
Enter command: 
Enter command:Connection closed by foreign host.
Sat Aug  3 12:18:26 CST 2013
server CMD xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;CMD4

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
CONNECTING TO xxx...
PROCESS CPL CONNECTED...
Enter command:RESP:0,sucess;
Enter command:
Enter command: 
Enter command:Connection closed by foreign host.

回答1:


You may find these links useful :

  • String Manipulation
  • Regular Expressions

And, to extract the response part from the whole string, you can use something like this :

echo `expr "${CMD}" : '\.*?(RESP)(:)(\\d)\'`


来源:https://stackoverflow.com/questions/18032840/getting-the-line-in-shell-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!