Apply if else to a live log WSO2 carbon log - wso2

I'm grepping a live log and need to compare the response time. If the response time is greater than 5000, "Response time greater then 5000" needs to be printed on screen preferable with the response time.
Current script :
tail -f request-response-logger.log| grep getSubscriberTypeResponse | awk -F 'RESPONSE_TIME:' '{print $2}'| awk -F ',BODY:<soapenv:Body' '{print $1}'
Output:
255.0
289.0
352.0
236.0
365.0
520.0
472.0
528.0
560.0
522.0
557.0
586.0
493.0
I tried below code snippet. But it didn't work
if [$(grep getSubscriberTypeResponse | awk -F 'RESPONSE_TIME:' '{print $2}'| awk -F ',BODY:<soapenv:Body' '{print $1}') -gt 100]; then echo "greater than 100"; fi

I tried below command
tail -f request-response-logger.log | awk -F'[,:]' '{ if($13 > 5000.0 ) { print } }'| grep 'TRANSACTION:response' | grep getSubscriberTypeResponse

Related

Regexp is not working as expected in unix

I am trying with below code and its not working as expcted. I am new to REGEX. Please share your ideas. Thanks in advance.
test.xml
<?xml version="1.0"?>
<audit>
<interfaces>
<interface_dtls>ABCD,ABCD 123</interface_dtls>
<interface_dtls>TESTING,123 TEST</interface_dtls>
</interfaces>
</audit>
Trying with below unix commands
#!/bin/bash
for line in `cat test.xml | grep -oP "(?<=interface_dtls>)[^<]+"`; do
echo $line --Displaying line only for debugging purpose
interface_code=`echo $line | awk -F ',' '{print $1}'`
prcdr_cd=`echo $line | awk -F ',' '{print $2}'`
hive -e "select * from table \
where sub_sys_cd='$interface_code' and data_prcdr_desc='$prcdr_cd';"
done
Actual "ECHO" output:
ABCD,ABCD
TESTING,123
Expected "ECHO" output:
ABCD,ABCD 123
TESTING,123 TEST
Becuse of missing info(info after space) my query is not working as expected.
Using xml_grep, the more recommended option for parsing, as grep is not not an XML aware tool.
$ xml_grep 'interface_dtls' file --text_only
ABCD,ABCD 123
TESTING,123 TEST
One could also use grep as pointed by anubhava over in comments. Probably not the best of ways to do it, but can done for a one-time debug. For proper functionality use any XML readable commands (e.g xmllint or xml_grep).
$ grep -oP "(?<=<interface_dtls>)[^<]+" xml_file
ABCD,ABCD 123
TESTING,123 TEST
The skeletal code for extracting the individual words from the command can be done as below. I will leave it up to you to tweak it as you need and do not use the outdated `` style command expansion, rather use $ wherever applicable.
#!/bin/bash
while read -r paramA paramB;
do
interface_code=$(echo $paramA | awk -F ',' '{print $1}')
prcdr_cd=$(echo $paramA | awk -F ',' '{print $2}')
echo $interface_code $prcdr_cd
done < <(xml_grep 'interface_dtls' file --text_only)
The xml_grep utility was mentioned in another answer. This uses XMLStarlet, which is also able to validate and modify XML files on the command line:
$ xml sel -t -v '//interface_dtls' -nl data.xml
ABCD,ABCD 123
TESTING,123 TEST
After little bit of research i am able to resolve the issue. But thanks to https://stackoverflow.com/users/5291015/inian , https://stackoverflow.com/users/4941495/kusalananda and https://stackoverflow.com/users/548225/anubhava for helpful insights.
test.xml
<?xml version="1.0"?>
<audit>
<interfaces>
<interface_dtls>ABCD,ABCD 123</interface_dtls>
<interface_dtls>TESTING,123 TEST</interface_dtls>
</interfaces>
</audit>
Before:
#!/bin/bash
for line in `cat test.xml | grep -oP "(?<=interface_dtls>)[^<]+"`; do
echo $line --Displaying line only for debugging purpose
interface_code=`echo $line | awk -F ',' '{print $1}'`
prcdr_cd=`echo $line | awk -F ',' '{print $2}'`
hive -e "select * from table \
where sub_sys_cd='$interface_code' and data_prcdr_desc='$prcdr_cd';"
done
After:
#!/bin/bash
IFS='$\n'
for line in `cat test.xml | grep -oP "(?<=interface_dtls>)[^<]+" | cut -d '>' -f 2 | cut -d '<' -f 1`; do
echo $line --Displaying line only for debugging purpose
interface_code=$(echo $line | awk -F ',' '{print $1}')
prcdr_cd=$(echo $line | awk -F ',' '{print $2}')
hive -e "select * from table \
where sub_sys_cd='$interface_code' and data_prcdr_desc='$prcdr_cd';"
done
"ECHO" output:
ABCD,ABCD 123
TESTING,123 TEST

Continue a bash script

After the success view of the host ( IPs ) I need to ping them in order to check if they are up. SIDS file contains 2 columns with hostnames. Are there any suggestions on how to Iimprove the code below?
#!/bin/bash
LINES=`cat /home/marko/SIDS | sed "s!/!-!g" | wc -l`
for (( i=1; i<=${LINES}; i++))
do
FIRSTIP=CPE-`sed -n "${i}{p;q}" /home/marko/SIDS | awk '{print $1}'| sed "s!/!-!g"`
SECONDIP=CPE-`sed -n "${i}{p;q}" /home/marko/SIDS | awk '{print $2}'| sed "s!/!-!g"`
COUNT=$( host ${FIRSTIP} | grep address | wc -l )
if [ $COUNT -gt 0 ]
then
echo success
else
echo ${SECONDIP}
fi
done
You can just use dig, to avoid searching the output of host:
IP=$(dig +short $SERVERNAME)
Then to check, if the host is alive:
if ping -q -c $IP >/dev/null 2>&1
then
echo "OK"
fi

Grep in bash with regex

I am getting the following output from a bash script:
INFOPLIST_FILE = MajorDomo/MajorDomo-Info.plist
and I would like to get only the path(MajorDomo/MajorDomo-Info.plist) using grep. In other words, everything after the equals sign. Any ideas of how to do this?
This job suites more to awk:
s='INFOPLIST_FILE = MajorDomo/MajorDomo-Info.plist'
awk -F' *= *' '{print $2}' <<< "$s"
MajorDomo/MajorDomo-Info.plist
If you really want grep then use grep -P:
grep -oP ' = \K.+' <<< "$s"
MajorDomo/MajorDomo-Info.plist
Not exactly what you were asking, but
echo "INFOPLIST_FILE = MajorDomo/MajorDomo-Info.plist" | sed 's/.*= \(.*\)$/\1/'
will do what you want.
You could use cut as well:
your_script | cut -d = -f 2-
(where your_script does something equivalent to echo INFOPLIST_FILE = MajorDomo/MajorDomo-Info.plist)
If you need to trim the space at the beginning:
your_script | cut -d = -f 2- | cut -d ' ' -f 2-
If you have multiple spaces at the beginning and you want to trim them all, you'll have to fall back to sed: your_script | cut -d = -f 2- | sed 's/^ *//' (or, simpler, your_script | sed 's/^[^=]*= *//')
Assuming your script outputs a single line, there is a shell only solution:
line="$(your_script)"
echo "${line#*= }"
Bash
IFS=' =' read -r _ x <<<"INFOPLIST_FILE = MajorDomo/MajorDomo-Info.plist"
printf "%s\n" "$x"
MajorDomo/MajorDomo-Info.plist

egrep string case

I have to grep from a file name temp which has something like this
Process State
BE_RP:1 [PL_2_3] Running
BE_RP:2 [PL_2_4] Running
BE_RP:3 [PL_2_5] Running
BE_RP:4 [PL_2_6] Running
FE_SCTP:0 [PL_2_3] Running
FE_SCTP:1 [PL_2_4] Running
BE_NMP:0 Not Running
OAM:0 Running
I need to write a egrep statement which will return the number of process which are in running or not running state.
awk '/^OAM/ { next } /Not Running[ \t]*$/{s++} END {print s, NR-s-1}' foo.txt
Prints <running> <not running>
Running
$ grep -v 'OAM' input | grep -cP '(?<!Not) Running\s*$'
6
Not Running
$ grep -v 'OAM' input | grep -cP 'Not Running\s*$'
1
sed '{
1 d
s/^[^:]*:[0-9]*[ ]*//
s/^[^]]*]//
s/^[ ]*//
}' input_file | sort | uniq -c
grep -P '^(?!OAM:0).*Running' temp | cut -f2 | wc -l

How to get an output formatted as "Interface: IP Address" from ifconfig on Mac

I am trying to get the following formatted output out of ifconfig:
en0: 10.52.30.105
en1: 10.52.164.63
I've been able to at least figure out how to get just the IP addresses (weeding out localhost) with the following command, but it's not sufficient for my requirements:
ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'
Thanks!
This works on FreeBSD, which is at the heart of an apple :-)
#!/bin/sh
for i in $(ifconfig -l); do
case $i in
(lo0)
;;
(*)
set -- $(ifconfig $i | grep "inet [1-9]")
if test $# -gt 1; then
echo $i: $2
fi
esac
done
On Debian/RHEL systems you can do the following ---
#!/bin/sh
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "Interface: IP : MASK : BROADCAST : HWADDR"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
for i in $(ifconfig -a| grep -v ^$| grep ^[a-z*] | awk '{print $1}')
do
case $i in
(lo)
;;
(*)
ip=`(/sbin/ifconfig $i | awk /'inet addr/ {print $2}' | cut -f2 -d":" )`
bcast=`(/sbin/ifconfig $i | awk /'Bcast/ {print $3}' | cut -f2 -d":" )`
mask=`(/sbin/ifconfig $i | awk /'inet addr/ {print $4}' | cut -f2 -d":" )`
hwaddr=`(/sbin/ifconfig $i | awk /'HWaddr/ {print $4,$5}' | cut -f2 -d" " )`
if [ -z $ip ]; then
ip="NA"
fi
if [ -z $bcast ]; then
bcast="NA"
fi
if [ -z $mask ]; then
mask="NA"
fi
if [ -z $hwaddr ]; then
hwaddr="NA"
fi
echo $i: $ip : $mask : $bcast : $hwaddr
;;
esac
done