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

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")

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 craete a NVMe with 4K sector size in KVM-QEMU

For testing purposes, I am creating a virtual machine as it follows:
qemu-img create -f qcow2 \
-o cluster_size=4096,preallocation=full \
/home/marcop/.libvirt/nvme-20G.qcow2 20G
qemu-system-x86_64 -machine q35,accel=kvm \
-m 4096 \
-smp 4 \
-cpu host \
-boot d \
-cdrom /var/lib/libvirt/isos/archlinux-2020.10.01-x86_64.iso \
-drive file=/home/marcop/.libvirt/nvme-20G.qcow2,if=none,aio=native,cache.direct=on,id=D24 \
-device nvme,drive=D24,serial=1234,logical_block_size=4096,physical_block_size=4096
When booted inside the machine, I use fdisk and nvme-cli to check the sector size, but it's always 512B.
pacman -Sy nvme-cli
fdisk -l /dev/nvme0n1
with output:
Disk /dev/nvme0n1: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: QEMU NVMe Ctrl
Units: sectors of 1 * 512 = 512 bytes
Sector size (logica/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Looking for the available sector size modes with nvme-cli (see here and here for details on NVMe)
nvme id-ns /dev/nvme0n1
return output:
NVME Identify Namespace 1:
nsze : 0x2800000
ncap : 0x2800000
nuse : 0x2800000
nsfeat : 0
nlbaf : 0
flbas : 0
mc : 0
dpc : 0
dps : 0
nmic : 0
rescap : 0
fpi : 0
dlfeat : 0
nawun : 0
nawupf : 0
nacwu : 0
nabsn : 0
nabo : 0
nabspf : 0
noiob : 0
nvmcap : 0
nsattr : 0
nvmsetid: 0
anagrpid: 0
endgid : 0
nguid : 00000000000000000000000000000000
eui64 : 0000000000000000
lbaf 0 : ms:0 lbads:9 rp:0 (in use)
Which indicates the only one sector profile exits. For comparison, the output of the same command issued for my physical NVMe returns
[...]
lbaf 0 : ms:0 lbads:9 rp:0x2 (in use)
lbaf 1 : ms:0 lbads:12 rp:0x1
Any help would be greatly appreciated!
run command to format with 4k. But beware that format will wipe out data.
nvme format /dev/nvme0n1 --lbaf=1

Recover informations from CSV files with my awk script

I have this CSV files :
Monday,linux,6,0.2
Tuesday,linux,0.25,0.2
Wednesday,linux,64,3
I create a little script that allow me to recover the informations from my csv
and to place them like this :
Day : Monday
OS : Linux
RAM : 6
CPU1 : 0.2
My script is :
#!/bin/bash
awk -F'[ ,;|.]' 'FNR==0{next}
FNR>1 {
print "DAY : " $1;
print "OS :\n " $2
print "RAM :\n " $3
print "CPU1 :\n " $4
}' mycsvfile.csv
But the result is :
DAY : Tuesday
OS :
linux
RAM :
0
CPU1 :
25
DAY : Wednesday
OS :
linux
RAM :
64
CPU1
Or I want :
DAY : Monday
OS : linux
RAM : 0.2
CPU 1 : 1
DAY : Tuesday
OS : linux
RAM : 0.25
CPU 1 : 0.2
DAY : Wednesday
OS : linux
RAM : 64
CPU 1 : 3
Can you tell me why my script doesn't works and why floats are not taken into account ?
Thank you !
Added tab and newline to same awk as Cyrus posted.
awk -F ',' '{
print "DAY :",$1
print "OS :",$2
print "RAM :",$3
print "CPU1 :",$4"\n"
}' OFS='\t' file
DAY : Monday
OS : linux
RAM : 6
CPU1 : 0.2
DAY : Tuesday
OS : linux
RAM : 0.25
CPU1 : 0.2
DAY : Wednesday
OS : linux
RAM : 64
CPU1 : 3
A more generic solution:
awk -F, 'BEGIN {split("DAY OS RAM CPU", header, " ")}{for (i=1;i<=4;i++) print header[i]":\t",$i;print ""}' t
DAY: Monday
OS: linux
RAM: 6
CPU: 0.2
DAY: Tuesday
OS: linux
RAM: 0.25
CPU: 0.2
DAY: Wednesday
OS: linux
RAM: 64
CPU: 3
More readable:
awk -F, '
BEGIN {split("DAY OS RAM CPU", header, " ")}
{
for (i=1;i<=4;i++)
print header[i]":\t",$i;
print ""
}' file

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

linux bash cut one row which starts with a certain string

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

Resources