How to split the denominator value from one column and store it in another column using perl? - perl

My example code output:
time | name | status | s_used | s_max |
+------------+-------------+-----------+------------+-----------+
| 1482222363 | asf | Closed | 0/16 | 0 |
| 1482222363 | as0 | Available | 4/16 | 4 |
I have attached the part of my output which is generated using perl cgi script and mysql database.
My query is how to take denominator value from the column s_used and store only the denominator values in the s_max column using perl.
3.I had attached the following part of code which i tried.
if($i == 4){
if(/s_used/){
print;
}
else{
chomp();
my($num,$s_max)=split /\//,$table_data{2}{'ENTRY'};
print $s_max;
}
}
Code Explanation:
$i == 4 is the column where should I store the variable.
I got time column from the sql database $time, name I got from $table_data{0}{'ENTRY'}, status from $table_data{1}{'ENTRY'}, s_used from $table_Data{2}{'ENTRY'}.
Expected output:
time | name | status | s_used | s_max |
+------------+-------------+-----------+------------+-----------+
| 1482222363 | asf | Closed | 0/16 | 16 |
| 1482222363 | as0 | Available | 4/16 | 16 |

Seems your code "my($num,$s_max)=split /\//,$table_data{2}{'ENTRY'};" is right.
Somehow the value $s_max at the time it's writing to the DB is incorrect. Since you did not post the portion of code to show the part $s_max writing back to the DB, you need to check what value is in $s_max (e.g. printing the $s_max value) at the time right before writing it back to DB. From there, please try to trace back why an incorrect value is assigned to $s_max. Then, the problem would be solved.

Related

Converting bytes to megabytes on the fly

In the PS command below I'm getting the name, creationTimeStamp, Disk_Size and storageBytes of yesterday's snapshots in my gcp project and outputting the data to a csv file (which is later converted to HTML and emailed):
$csv = gcloud --project $gcpProject compute snapshots list --format="csv(name,creationTimestamp,diskSizeGb,storageBytes)" --filter="creationTimestamp.date('%Y-%m-%d')=$yesterday" | Out-File C:\data.csv
The result looks something like this (the number of snapshots displayed varies):
+---------------------------+-------------------------------+--------------+---------------+
| NAME | CREATION_TIMESTAMP | DISK_SIZE_GB | STORAGE_BYTES |
+---------------------------+-------------------------------+--------------+---------------+
| snapshot1-us-central1 | 2019-10-24T19:24:09.061-07:00 | 50 | 1250586048 |
| snapshot2-data-us-east1 | 2019-10-24T19:01:49.791-07:00 | 150 | 425018600 |
+---------------------------+-------------------------------+--------------+---------------+
This is good except that the STORAGE_BYTES data is all in bytes thus making it hard to read. How can I record this data in MB instead in the csv file (or just replacing this data in the csv file that's in bytes with MB)
$data=Get-Content C:\data.csv
$NewData=#($data[0..2])#Adding Header in a new Variable
$Data[3..$($data.Count - 2)]|Foreach{
$startRange=$_.LastIndexOf("| ")+1
$length=$_.LastIndexOf(" |") - $_.LastIndexOf("| ")
#Converting Bytes into MB and replacing in the line
$NewData+=$_.Replace(($_.Substring($startRange,$length)).Trim(),"$([math]::Round(($_.Substring($startRange,$length)/1MB),2)) ")
}
#Adding last line
$NewData+=$data[$data.Count - 1]
#Modifying exsiting file
$NewData |Set-Content C:\data.csv -Force
I think, it should be simple using GCP PowerShell cmdlets.
Get-GceSnapshot -Project $gcpProject
I never used it, but you can use calculated properties to convert the size in oneline, an example below
Get-Process | Select-Object -First 3 -Property Name,#{E={$_.STORAGE_BYTES/1024};L='Size'}
PS: assuming the size property name is STORAGE_BYTES
https://googlecloudplatform.github.io/google-cloud-powershell/#/google-compute-engine/GceSnapshot/Get-GceSnapshot

Collecting Unique Items from large data set over multiple text files

I am using PowerShell to collect lists of names from multiple text files. May of the names in these files are similar / repeating. I am trying to ensure that PowerShell returns a single text file with all of the unique items. In looking at the data it looks like the script is gathering 271/296 of the unique items. I'm guessing that some of the data is being flagged as duplicates when it shouldn't, any suggestions?
#Take content of each file (all names) and add unique values to text file
#for each unique value, create a row & check to see which txt files contain
function List {
$nofiles = Read-Host "How many files are we pulling from?"
$data = #()
for ($i = 0;$i -lt $nofiles; $i++)
{
$data += Read-Host "Give me the file name for file # $($i+1)"
}
return $data
}
function Aggregate ($array) {
Get-Content $array | Sort-Object -unique | Out-File newaggregate.txt
}
#SCRIPT BODY
$data = List
aggregate ($data)
I was expecting this code to catch everything, but it's missing some items that look very similar. List of missing names and their similar match:
CORPINZUTL16 MISSING FROM OUTFILE
CORPINZTRACE MISSING FROM OUTFILE
CORPINZADMIN Found In File
I have about 20 examples like this one. Apparently the Get-Content -Unique is not checking every character in a line. Can anyone recommend a better way of checking each line or possibly forcing the get-character to check full names?
Just for demonstration this line creates 3 txt files with numbers
for($i=1;$i -lt 4;$i++){set-content -path "$i.txt" -value ($i..$($i+7))}
1.txt | 2.txt | 3.txt | newaggregate.txt
1 | | | 1
2 | 2 | | 2
3 | 3 | 3 | 3
4 | 4 | 4 | 4
5 | 5 | 5 | 5
6 | 6 | 6 | 6
7 | 7 | 7 | 7
8 | 8 | 8 | 8
| 9 | 9 | 9
| | 10 | 10
Here using Get-Content with a range [1-3] of files
Get-Content [1-3].txt | Sort-Object {[int]$_} -Unique | Out-File newaggregate.txt
$All = Get-Content .\newaggregate.txt
foreach ($file in (Get-ChildItem [1-3].txt)){
Compare-Object $All (Get-Content $file.FullName) |
Select-Object #{n='File';e={$File}},
#{n="Missing";e={$_.InputObject}} -ExcludeProperty SideIndicator
}
File Missing
---- -------
Q:\Test\2019\05\07\1.txt 9
Q:\Test\2019\05\07\1.txt 10
Q:\Test\2019\05\07\2.txt 1
Q:\Test\2019\05\07\2.txt 10
Q:\Test\2019\05\07\3.txt 1
Q:\Test\2019\05\07\3.txt 2
there are two ways to achieve this one is using select-object -Unique which works when data is not sorted and can be used for small data or lists.
When dealing with large files we can use get-Unique command which works with sorted input, if input data is not sorted then it will give wrong results.
Get-ChildItem *.txt | Get-Content | measure -Line #225949
Get-ChildItem *.txt | Get-Content | sort | Get-Unique | measure -Line #119650
Here is my command for multiple files :
Get-ChildItem *.txt | Get-Content | sort | Get-Unique >> Unique.txt

Pushing into array Selenium IDE 2019

I'm trying to populate an array automatically.
These stepes just returns an array containing "foo". The "bar"-string I'm trying to push into it doesnt seem to be included.
execute script | return ["foo"] | testVar
execute script | ${testVar}.push("bar") |
echo | ${testVar}
What am I doing wrong?
I use this approach to update an array in Selenium IDE
execute script | return ['foo'] | testVar
execute script | return [${testVar}, 'bar'] | testVar
echo | ${testVar}
UPDATE
Previous solution is not correct because of wrong value will be stored in the variable testVar. It will be array of array of arrays. So here is the correct one:
execute script | return ['foo'] | testVar
execute script | tmp = ${testVar}; tmp.push('bar'); return tmp; | testVar
echo | ${testVar}

Pure Data from print~ to print second element with print

With the following program, I get a big list out of print~.
I want to a get the second value from this list.
How to get this second value and print it with print and not print~?
[print~] will always print the entire signal-block (in your case 128 values).
to get a specific sample, you could instead use a table, feed the monitored signal into it, and retrieve the value(s) you are interested:
...
|
[tabsend~ $0-foo]
[table $0-foo 128]
[2(
|
[tabread $0-foo]
|
[print]
note: unlike with [print~], which will only generate output at the next signal block, this will output the data immediately (that is: it will take the signal data from the last signal block).
to get the next signal-block, you can replace the lower part of the suggested solution with something like the following:
[bang~] [2(
| |
| [r $0-next] |
| | |
[spigot] [t b f]
| | |
[t f b] [1( |
| | | |
| [0( [s $0-next] |
| | |
| [s $0-next] |
| |
[float ]
|
[tabread $0-foo]
|
...
(the patch uses [s/r $0-next] to avoid cross-over connection lines in the ascii graphics; in practice you probably want to use straight connections instead)
it's probably best to encapsulate the entire thing into an abstraction.

Selenium IDE - Array add values

Let's say I have two String variables: var1 and var2.
Is there any command on Selenium IDE (maybe storeEval with some javascript code), with which I can create an array and add the 2 variable values to it? Example:
var1 = "abc"
var2 = "def"
array = ("abc","def")
P.S: the array cannot have a fixed length. In this case I have just 2 variables, but in other scenarios I may have more than 10 variables, so I would need to create a loop and add all 10 variable values to the array.
Thanks!
It's pretty simple but isn't obvious
storeEval | ['one','two'] | array
storeEval | storedVars['array'][1] | second
echo | ${second}
Or simpler but with much less safety
storeEval | ['one','two'] | array
echo | javascript{storedVars['array'][1]}
Adding new item dynamically
getEval | storedVars['array'].push('three')
You can make loop using selenium IDE flow control for example. Like:
storeEval | 0 | i
while | storedVars['i']<storedVars['array'].length
echo | javascript{storedVars['array'][storedVars['i']]}
storeEval | ${i}+1 | i
endWhile
I hope it will help