hi I looked the others questions and tried some things but i couldnt manage to solve it.
My string is that
:CONNECTING TO CPL... PROCESS CPL CONNECTED... Enter command: Enter
command:RESP:0,CPrL-E1002:RESPMSG,Invalid session ID.; Enter
command:RESP:0,CPL-E1014:RESPMSG,System internal error; Enter command:
Connection closed by foreign host.
how Can I take RESP:0,CPL-E1014:RESPMSG,System internal error;
this line? Also RESP:0,CPL-E1014:RESPMSG,System internal error; this line wıll change according the request .So I must take only from RESP: to ; for the second line
Assuming that the string is in a variable named $str
, and that there are no other stars *
in the string, you could use an awk filter, like this:
awk -F\* 'NF>1{print $2}' <<< "$str"
Here we use the -F
option to set the field separator to the star, and if we find a line with more than one elements (one star or more), we print the second field.
output:
$ echo "$str"
:CONNECTING TO CPL... PROCESS CPL CONNECTED... Enter command: Enter
command:*RESP:0,CPrL-E1002:RESPMSG,Invalid session ID.;* Enter
command:AREE:0,CPL-E1014:RESPMSG,System internal error; Enter command:
Connection closed by foreign host.
$
$ awk -F\* 'NF>1{print $2}' <<< "$str"
RESP:0,CPrL-E1002:RESPMSG,Invalid session ID.;
You can use expr and regular expressions, please refer to this answer first. Have a look at this code :
#!/bin/bash
# foo.sh
# Searches the string for a "RESP:#" message, the # sign is a digit.
string='CONNECTING TO CPL... PROCESS CPL CONNECTED... Enter command:RESP:0,CNDB-0,CPL-0,EMA-0:RESPMSG,success; Enter command: Enter command:
RESP:1,CPL-0,EMA-0:RESPMSG,success; Enter command: Connection closed by foreign host'
regex='RESP\:[0-9]\,CPL\-0\,EMA\-0\:RESPMSG\,[a-zA-Z0-9]*\;'
line=`expr "$string" : '.*\(RESP\:[0-9]\,CPL\-0\,EMA\-0\:RESPMSG\,[a-zA-Z0-9]*\;\)'` # Search the string for the desired line.
respCode=${line:5:1} # Extract RESP:# value using substring...
respMessage=${line:27}
echo $line
echo 'Received response code : '$respCode
echo 'Received response message : '$respMessage
Try doing this using only grep :
$ grep -oP 'command:\KRESP:.*?System internal error;' test.txt
RESP:0,CPL-E1014:RESPMSG,System internal error;
test=":CONNECTING TO CPL... PROCESS CPL CONNECTED... Enter command: Enter command:RESP:0,CPrL-E1002:RESPMSG,Invalid session ID.; Enter command:RESP:0,CPL-E1014:RESPMSG,System internal error; Enter command:Connection closed by foreign host."
str= echo $test | cut -d";" -f2
echo $test | cut -d";" -f2
echo ${str:15:100}";"
Here I assumed that your response string comes every time after " Enter command:". 100 is for the maximum length of your response string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.