linux bash cut one row which starts with a certain string - linux

Good day,
im using linux bash commands to extract certain data of each sip account and put them next to each other.
i have an array called $peers that i put all 1000 sips into and now i need to for loop through them to set every sip to its useragent.
what i have so far is
#! /bin/bash
peers="$(asterisk -rx "sip show peers" | cut -f1 -d" " | cut -f1 -d"/" "=")" "= " asterisk -rx "sip show peer " $peer | cut -f2 -d"Useragent"
for peer in $peers do
echo $peers
done
#echo $peers
I need to extract a row from a collection of rows that starts with "Useragent"
I start by running asterisk -rx "sip show peer 101" and that gives me the result below
* Name : 101
Description :
Secret : <Set>
MD5Secret : <Not set>
Remote Secret: <Not set>
Context : outgoing
Record On feature : automon
Record Off feature : automon
Subscr.Cont. : <Not set>
Language :
Tonezone : <Not set>
AMA flags : Unknown
Transfer mode: open
CallingPres : Presentation Allowed, Not Screened
Callgroup :
Pickupgroup :
Named Callgr :
Nam. Pickupgr:
MOH Suggest :
Mailbox :
VM Extension : asterisk
LastMsgsSent : 0/0
Call limit : 0
Max forwards : 0
Dynamic : Yes
Callerid : "" <>
MaxCallBR : 384 kbps
Expire : 23
Insecure : no
Force rport : Yes
Symmetric RTP: Yes
ACL : No
DirectMedACL : No
T.38 support : No
T.38 EC mode : Unknown
T.38 MaxDtgrm: -1
DirectMedia : Yes
PromiscRedir : No
User=Phone : No
Video Support: No
Text Support : No
Ign SDP ver : No
Trust RPID : No
Send RPID : No
Subscriptions: Yes
Overlap dial : Yes
DTMFmode : rfc2833
Timer T1 : 500
Timer B : 32000
ToHost :
Addr->IP : xxx.xxx.xxx.xxx:5060
Defaddr->IP : (null)
Prim.Transp. : UDP
Allowed.Trsp : UDP
Def. Username: 101
SIP Options : (none)
Codecs : (gsm|ulaw|alaw|g729|g722)
Codec Order : (gsm:20,g722:20,g729:20,ulaw:20,alaw:20)
Auto-Framing : No
Status : OK (9 ms)
Useragent : UniFi VoIP Phone 4.6.6.489
Reg. Contact : sip:101#xxx.xxx.xxx.xxx:5060;ob
Qualify Freq : 60000 ms
Keepalive : 0 ms
Sess-Timers : Accept
Sess-Refresh : uas
Sess-Expires : 1800 secs
Min-Sess : 90 secs
RTP Engine : asterisk
Parkinglot :
Use Reason : No
Encryption : No
Now i need to cut this part Useragent : UniFi VoIP Phone 4.6.6.489
and display it as 101 : UniFi VoIP Phone 4.6.6.489
any help would be much appreciated
Thank you. that top answer worked perfectly. this is my solution now.
peer="$(asterisk -rx "sip show peers" | cut -f1 -d" " | cut -f1 -d"/" )"
for peer in $peers do
output= "$(asterisk -rx "sip show peer $peers" | sed -nE '/Useragent/ s/^[^:]+/101 /p')"
echo $output
done
But is is still giving issue, my problem is the loop of the variables

With sed:
... | sed -nE '/Useragent/ s/^[^:]+/101 /p'
/Useragent/ matches line(s) with Useragent it
s/^[^:]+/101 substitutes the portion from start till : (exclusive) with 101

Related

Any Windows alternative to Unix utility Ethtool for autonegotiation of ethernet?

I have to implement the windows equivalent of the following :
for iface in `ifconfig -a | sed 's/[ \t].*//;/^$/d'`;
do echo \"ethtool back on $iface\";
ethtool -s $iface autoneg on ;
done
How can this be done in Windows, through command line?
Here is an example of getting and setting speed/duplex/autonegotiation on single named interface using PowerShell:
PS> Get-NetAdapter
Name InterfaceDescription ifIndex Status MacAddress LinkSpeed
---- -------------------- ------- ------ ---------- ---------
Ethernet Intel(R) Ethernet Connection (4) I219-V 26 Disconnected E8-6A-64-3B-28-1A 0 bps
Wi-Fi Intel(R) Dual Band Wireless-AC 8265 10 Up 18-1D-EA-B4-7E-0E 300 Mbps
PS> Get-NetAdapterAdvancedProperty -Name Ethernet -DisplayName "Speed & Duplex" | fl DisplayName, DisplayValue, ValidDisplayValues,Name
DisplayName : Speed & Duplex
DisplayValue : Auto Negotiation
ValidDisplayValues : {Auto Negotiation, 10 Mbps Half Duplex, 10 Mbps Full Duplex, 100 Mbps Half Duplex...}
Name : Ethernet
PS> Set-NetAdapterAdvancedProperty -Name Ethernet -DisplayName "Speed & Duplex" -DisplayValue "100 Mbps Half Duplex"
PS> Get-NetAdapterAdvancedProperty -Name Ethernet -DisplayName "Speed & Duplex" | fl DisplayName, DisplayValue
DisplayName : Speed & Duplex
DisplayValue : 100 Mbps Half Duplex
Parameterizing and adding a loop to iterate over all the interfaces, I will leave that task to you.

How to display the name of the files that is in the process of analyse in my bash script?

I have many csv files like this :
FRAME_NAME1,LPAR_NAME1,25,1.0,2
FRAME_NAME2,LPAR_NAME2,12,0.8,1
FRAME_NAME1,LPAR_NAME1,25,1.0,2
FRAME_NAME3,LPAR_NAME3,0,null,0
FRAME_NAME3,LPAR_NAME3,0,null,0
FRAME_NAME3,LPAR_NAME3,0,null,0
FRAME_NAME1,LPAR_NAME1,25,1.0,2
FRAME_NAME1,LPAR_NAME1,25,1.0,2
FRAME_NAME2,LPAR_NAME2,25,1.0,2
I use this script :
OLDIFS=$IFS
IFS=","
while read FRAME LPARS RAM CPU1 CPU2
do
if [[ $FRAME != $PREV ]]
then
PREV="$FRAME"
echo -e "\e[1;33m$FRAME \
======================\e[0m\n"
fi
echo -e "LPARS : \t$LPARS\n\
RAM : \t$RAM\n\
CPU1 : \t$CPU1\n\
CPU2 : \t$CPU2\n"
done < <(sort "$1")
To display those informations like this :
FRAME_NAME 1 =======================
LPAR_NAME : LPAR_NAME1
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
LPAR_NAME : LPAR_NAME1
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
LPAR_NAME : LPAR_NAME1
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
LPAR_NAME : LPAR_NAME1
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
FRAME_NAME 2 =======================
LPAR_NAME : LPAR_NAME2
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
LPAR_NAME : LPAR_NAME2
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
FRAME_NAME3 =======================
LPAR_NAME : LPAR_NAME3
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
LPAR_NAME : LPAR_NAME3
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
LPAR_NAME : LPAR_NAME3
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
But as I have many csv, I don't know which file is analyse. So when the informations of all my csv are displayed, I would like to display the name of the file to belongs thoses informations :
Something like this :
FILE : my_csv_1.csv
FRAME_NAME 1 =======================
LPAR_NAME : LPAR_NAME1
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
LPAR_NAME : LPAR_NAME1
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
LPAR_NAME : LPAR_NAME1
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
LPAR_NAME : LPAR_NAME1
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
FILE : my_csv_2.csv
FRAME_NAME 2 =======================
LPAR_NAME : LPAR_NAME2
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
LPAR_NAME : LPAR_NAME2
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
FILE : my_csv_3.csv
FRAME_NAME3 =======================
LPAR_NAME : LPAR_NAME3
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
LPAR_NAME : LPAR_NAME3
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
LPAR_NAME : LPAR_NAME3
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
I have seen the FILENAME variable with awk, but I don't know how to put this variable on my script...
Can you show me how to do this ?
Your script by the end of the while loop reads from a parameter that is $1 that I assume has to be the file name with which Your script got called.
If so, You can just modify Your script to be like:
OLDIFS=$IFS
IFS=","
echo -e "FILENAME : $1\n"
while read FRAME LPARS RAM CPU1 CPU2
do
if [[ $FRAME != $PREV ]]
then
PREV="$FRAME"
echo -e "\e[1;33m$FRAME \
======================\e[0m\n"
fi
echo -e "LPARS : \t$LPARS\n\
RAM : \t$RAM\n\
CPU1 : \t$CPU1\n\
CPU2 : \t$CPU2\n"
done < <(sort "$1")

Replace/Update column value from text document in bash

I got a question on how can i update an specific column value from a row in a text document in bash.
So far I do this:
In this case I'm trying to update the 4th column in a line from an specific account number. take the current value, sum with a new value inserted by the user and then replace that current value with the result.
Open the script with two variable $1 holds the account I ll be looking for while $2 the file name.
The text document has information like this:
11101 : CAJA GENERAL : 111 : 0
11102 : CAJA CHICA : 111 : 0
112 : BANCOS : 11 : 0
11201 : CUENTAS CORRIENTES : 112 : 0
1120101 : Banco Agrícola S.A. : 11201 : 20
1120102 : Banco Hipotecario S.A. : 11201 : 0
11202 : CUENTAS DE AHORRO : 112 : 0
1120201 : Banco Agrícola S.A. : 11202 : 0
I use this code to find the correct row and assign the current value to the var "act" and aggregate it with another new value
read -p "Inserte monto" insert
act=$(grep -w "^$1" $2 | cut -d":" -f4)
vare=$(($act + $insert))
But then I need to place this new value to the exact column/row I took the original value.
How do i exactly do it? I'm quite exhausted from a long day of traveling and now I want to finish this and go to bed. Anyone could give me an idea or solution? I'd really appreciate any help right now.
EDIT:
Ok.. I went and try AWK.
Found this to "replace" text
awk -F':' -vOFS=' ' '{ $4 = "$vare"}1'
So far... once I enter that line the execution of the script stops.. or I dont know what but it doesn't continue anymore, nor shows any error.
Am I doing anything wrong?
EDIT 2:
Expected Input.
After a successful update I want the selected account to have the updated value in a existing document in my directory, the solutions so far allow me to see the updates within terminal, but is the original document the one I need to see the changes. Thanks Ed Morton for the tip
Maybe try awk:
$ cat file
11101 : CAJA GENERAL : 111 : 0
11102 : CAJA CHICA : 111 : 0
112 : BANCOS : 11 : 1
11201 : CUENTAS CORRIENTES : 112 : 0
1120101 : Banco Agrícola S.A. : 11201 : 20
1120102 : Banco Hipotecario S.A. : 11201 : 0
11202 : CUENTAS DE AHORRO : 112 : 0
1120201 : Banco Agrícola S.A. : 11202 : 0
$ insert=10
$ ident=112
$ awk -i inplace -F: "/^$ident /{gsub(/([0-9]+)$/, \$4+ $insert)};{print}" file
-or-
$ awk -i inplace -v ident="$ident" -v insert="$insert" '$1==ident{sub(/[0-9]+$/, $NF+insert)} 1' file
$ cat file
11101 : CAJA GENERAL : 111 : 0
11102 : CAJA CHICA : 111 : 0
112 : BANCOS : 11 : 11
11201 : CUENTAS CORRIENTES : 112 : 0
1120101 : Banco Agrícola S.A. : 11201 : 20
1120102 : Banco Hipotecario S.A. : 11201 : 0
11202 : CUENTAS DE AHORRO : 112 : 0
1120201 : Banco Agrícola S.A. : 11202 : 0
You can do it with sed:
id=$1
sed -i "/^$id *:/ s/[0-9]\+$/$vare/" $2

Extract Part of String using cut/awk or anything

[DOSLB] : [dmob7h-002.on.bell.ca] : [61421820100992016102414274381420414330] : [[ACTIVE] ExecuteThread: '15' for queue: 'weblogic.kernel.Default (self-tuning)'] : [ca.bell.tv.doslb.infrastructure.logging.LoggingAspect] : [Ending execution of the class: ca.bell.tv.doslb.application.webservice.impl.RetrieveLocalSubscriberDelegateImpl] : [Method: getRetrieveLocalSubscriber[Call ended at: 2016-10-24 14:27:44.150] : [lasted 0 sec, 305 ms] : [WITHINLIMITS]]
I wanted to extract getRetrieveLocalSubscriber from the above string. But I can not be specific with its position and also the string since it is a service name so it will change by time in the log and the position may change but it will be in the same format, [Method: getRetrieveLocalSubscriber[Call ended at: 2016-10-24 14:27:44.150] this portion will always be the same.
and I also wanted to extract the lasted 0 sec part but the problem is the seconds will always change.
I want the output like getRetrieveLocalSubscriber in one variable and
lasted 0 sec in another variable
I have tried awk command
cat out_log.txt | awk -F '[:]' '{print $11}'
which is giving output is getRetrieveLocalSubscriber[Call ended at
Try something like this:
echo "[DOSLB] : [dmob7h-002.on.bell.ca] : [61421820100992016102414274381420414330] : [[ACTIVE] ExecuteThread: '15' for queue: 'weblogic.kernel.Default (self-tuning)'] : [ca.bell.tv.doslb.infrastructure.logging.LoggingAspect] : [Ending execution of the class: ca.bell.tv.doslb.application.webservice.impl.RetrieveLocalSubscriberDelegateImpl] : [Method: getRetrieveLocalSubscriber[Call ended at: 2016-10-24 14:27:44.150] : [lasted 0 sec, 305 ms] : [WITHINLIMITS]]" | perl -ne '/Method: getRetrieveLocalSubscriber\[Call ended at: (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\.(\d+?)\] : \[lasted (\d+?) sec, (\d+?) ms\]/ && print "Call end: $1-$2-$3 $4:$5:$6.$7, lasted for $8s $9ms";'
Call end: 2016-10-24 14:27:44.150, lasted for 0s 305ms
Or, if such strings are in a file:
cat test.log | perl -ne '/Method: getRetrieveLocalSubscriber\[Call ended at: (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\.(\d+?)\] : \[lasted (\d+?) sec, (\d+?) ms\]/ && print "Call end: $1-$2-$3 $4:$5:$6.$7, lasted for $8s $9ms";'
This regular expression also accepts different "call end" time. You can replace
(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\.(\d+?)
part in this regexp with 2016-10-24 14:27:44.150 and $8 and $9 with $1 and $2 respectively to match only strings with
Call ended at: 2016-10-24 14:27:44.150
substrings.
you can try something like this;
grep -o -P '(?=\[Method: getRetrieveLocalSubscriber).*(?<=ms])' yourFile
or
grep -o -P '(?=\[Call).*(?<=ms])' yourFile
Eg;
user#host$ grep -o -P '(?=\[Method: getRetrieveLocalSubscriber).*(?<=ms])' test
[Method: getRetrieveLocalSubscriber[Call ended at: 2016-10-24 14:27:44.150] : [lasted 0 sec, 305 ms]
user#host$ grep -o -P '(?=\[Call).*(?<=ms])' test
[Call ended at: 2016-10-24 14:27:44.150] : [lasted 0 sec, 305 ms]

IPMItool sel command on PER610 does not provide detailed information

ipmitool sel elist
on R610 output:
1 | 08/01/2011 | 23:18:11 | Event Logging Disabled SEL | Log area reset/cleared | Asserted
2 | Pre-Init Time-stamp | Physical Security Intrusion | General Chassis intrusion | Asserted
3 | Pre-Init Time-stamp | Physical Security Intrusion | General Chassis intrusion | Deasserted
4 | 01/31/2012 | 11:32:50 | Temperature #0x30 | Upper Critical going high
on R810 its:
Severity : Normal
Date and Time : System Boot
Description : The chassis is closed while the power is On.
Event Data : 0x80 0x02 0xff
I am concern about severity of message. I am developing a code which will send an email if the message is critical. But in the case of R610 there is no way to found severity of message.
If you're trying to read the actual data from the SEL then you need to use the ipmitool sel get command and not the ipmitool sel elist command.
the ipmitool sel get command returns the detailed breakdown of the information in the event log for the item in question.
e.g. from one of my own systems:
machine:/ # ipmitool sel get 0x2c
SEL Record ID : 002c
Record Type : 02
Timestamp : 02/13/2012 17:49:21
Generator ID : 0021
EvM Revision : 04
Sensor Type : Voltage
Sensor Number : 60
Event Type : Threshold
Event Direction : Assertion Event
Event Data : 02ffff
Description : Lower Critical going low

Resources