How to split multiple datas from multiple lines in perl - perl

Firstly, I have the data below:
*********************************************************************
TEST CASE
*********************************************************************
test results for the last 1 run(s)
TEST TITLE GROUP PRIO R-STAT R-TIME VERDICT VERDICT-TEXT
------------------------- ---------------------------------------- -------- ---- ------ ------------ -------- --------------------------------------------------
TESTCASE1 TC1 ABC 1 PASS 00:00:09.572 PASS nothing
TESTCASE2 TC2 DEF 2 PASS 00:00:01.650 PASS nothing
----------------------------------------------------------------------------------------------------
NUMBER OF : 2
NUMBER OF TC: 2
*********************************************************************
VERDICT: PASS
*********************************************************************
I would like to perform lines:
TESTCASE1 TC1 ABC 1 PASS 00:00:09.572 PASS nothing
TESTCASE2 TC2 DEF 2 PASS 00:00:01.650 PASS nothing
and split first lines and last lines.
How can I do that?
ADDITIONAL:
Sorry for too late reply, i want to split
*********************************************************************
TEST CASE
*********************************************************************
test results for the last 1 run(s)
TEST TITLE GROUP PRIO R-STAT R-TIME VERDICT VERDICT-TEXT
------------------------- ---------------------------------------- -------- ---- ------ ------------ -------- --------------------------------------------------
just handled two lines
TESTCASE1 TC1 ABC 1 PASS 00:00:09.572 PASS nothing
TESTCASE2 TC2 DEF 2 PASS 00:00:01.650 PASS nothing
i use while loop to check line by line, the problem is it will be looped all of lines whereas i want two lines only.

You can try this to get first and last line from file:
cat filename | grep "^TESTCASE" | sed -n '1p;$p'

Related

select total cpu collectd multiple hosts

I am using collectd and influx for monitoring a cluster of 200 cores.
I would like to create a gauge in grafana which is adding all load_shortterm value of all hosts to see the total usage of the cluster.
My structure looks like this :
name: load_shortterm
time host metric type value
---- ---- ------ ---- -----
1601891780201909599 cpu007.cluster load 0
1601891790145618383 cpu001.cluster load 2
1601891790163106767 cpu002.cluster load 0.03
1601891790167701326 cpu009.cluster load 0
So I want to have a request which will answer 2.03 in this case.
I don't understand how to get last values for each host and sum it. I tried this :
select sum(*) from load_shortterm where "host" =~ /^*.cluster/
But it returns a sum of all values.
Can you please help me ?
Thanks,
RB
SUM all LAST values with subquery, e.g.:
SELECT SUM(last)
FROM (
SELECT LAST(value)
FROM load_shortterm
WHERE "host" =~ /^*.cluster/
)
Thanks for the reply, the subquery returns the last value and not last values for each host :
> select last(value) from load_shortterm where "host" =~ /^*.cluster/
name: load_shortterm
time last
---- ----
1605863555866457205 0
With the full query it is the same result, it seems it does not take all last values but only the last one :
> select sum(last) from (select last(value) from load_shortterm where "host" =~ /^*.cluster/)
name: load_shortterm
time last
---- ----
1605863555866457205 0

PowerShell - xml files with conflicting multiple namespaces on the same element name

Related to PowerShell 5.1
I was playing around with XML to show how to handle conflicting namespaces. Here's the example I created:
<Employees>
<ms:Employee id='1' xmlns:ms="MicrosoftEmployees">
<FirstName>Bill</FirstName>
<LastName>Gates</LastName>
</ms:Employee>
<ms:Employee id='2' xmlns:ms="MicrosoftEmployees">
<FirstName>Paul</FirstName>
<LastName>Allen</LastName>
</ms:Employee>
<ap:Employee id='1' xmlns:ap="AppleEmployees">
<Name>Steve Jobs</Name>
</ap:Employee>
<ap:Employee id='2' xmlns:ap="AppleEmployees">
<Name>Steve Wozniak </Name>
</ap:Employee>
</Employees>
The scenario might be combining data from two different companies.
PowerShell demonstration program:
cls
$filename = "c:\XMLClass\IntroSamples\Sample05_Simpler_Namespace.xml"
[xml]$xmlDoc = Get-Content $filename
$xmlDoc.Employees.Employee[0]
$xmlDoc.Employees.Employee[1]
$xmlDoc.Employees.Employee[2]
$xmlDoc.Employees.Employee[3]
Output:
id ms FirstName LastName
-- -- --------- --------
1 MicrosoftEmployees Bill Gates
2 MicrosoftEmployees Paul Allen
1
2
Is there anyway to get a more logical output?
It seems like PowerShell locks into the first schema it sees for the Employee element, then cannot show the Name element of the Apple employees. This actually makes sense, but I was just checking to see if there is something fancier to handle this that I might be missing.
I know I could use SelectSingleNodes and XPath, but was just trying to see if and how PowerShell could handle this "out of the box".
If I reverse the code:
$xmlDoc.Employees.Employee[2]
$xmlDoc.Employees.Employee[3]
$xmlDoc.Employees.Employee[1]
$xmlDoc.Employees.Employee[0]
Then the output is:
id ap Name
-- -- ----
1 AppleEmployees Steve Jobs
2 AppleEmployees Steve Wozniak
1 ms:Employee
2 ms:Employee
Use format list to see all the properties. Format-table doesn't handle different sets of properties well.
$xmldoc.employees.employee | format-list
id : 1
ms : MicrosoftEmployees
FirstName : Bill
LastName : Gates
id : 2
ms : MicrosoftEmployees
FirstName : Paul
LastName : Allen
id : 1
ap : AppleEmployees
Name : Steve Jobs
id : 2
ap : AppleEmployees
Name : Steve Wozniak

Powershell replacing string with multiple values

I am new to powershell. Here are some code examples will help me to explain:
The first example gives the correct output I want which is a list of values, two values in this example, under OrganizationalUnitDistinguishedNames
PS C:\Users\Administrator\Desktop> $test=Get-APSDirectoryConfigList -DirectoryName test.com
PS C:\Users\Administrator\Desktop> $test
CreatedTime DirectoryName OrganizationalUnitDistinguishedNames ServiceAccountCredentials
----------- ------------- ------------------------------------ -------------------------
12/4/2017 9:26:50 AM test.com {OU=t1,DC=acc, OU=t2,DC=test} Amazon.AppStream.Model.ServiceAccountCredentials
PS C:\Users\Administrator\Desktop> $test.OrganizationalUnitDistinguishedNames
OU=t1,DC=acc
OU=t2,DC=test
However, the following command treats two values "OU=t2,DC=test,OU=t1,DC=acc" as a single string. What is the correct syntax to create with two separate values instead of a single string? I have tried different ways (with or without double quotes), they don't work.
PS C:\Users\Administrator\Desktop> $test=New-APSDirectoryConfig -DirectoryName test.com -OrganizationalUnitDistinguishedName "OU=t2,DC=test,OU=t1,DC=acc" -ServiceAcco
untCredentials_AccountName TEST\serviceaccount -ServiceAccountCredentials_AccountPassword secret_password
PS C:\Users\Administrator\Desktop> $test
CreatedTime DirectoryName OrganizationalUnitDistinguishedNames ServiceAccountCredentials
----------- ------------- ------------------------------------ -------------------------
12/4/2017 9:33:25 AM test.com {OU=t2,DC=test,OU=t1,DC=acc} Amazon.AppStream.Model.ServiceAccountCredentials
PS C:\Users\Administrator\Desktop> $test.OrganizationalUnitDistinguishedNames
OU=t2,DC=test,OU=t1,DC=acc
Try:
$OUDNArray = #("OU=t2,DC=test","OU=t1,DC=acc")
$test=New-APSDirectoryConfig -DirectoryName test.com -OrganizationalUnitDistinguishedName $OUDNArray -ServiceAccountCredentials_AccountName TEST\serviceaccount -ServiceAccountCredentials_AccountPassword T3st12345
According to the AWS Appstream Docs:
Parameters
-OrganizationalUnitDistinguishedName <String[]> The distinguished names of the organizational units for computer accounts.
Required? False Position? Named Accept pipeline input? False
The OrganizationalUnitDistinguishedName accepts an array.

use strings in PostgreSQL as variables to find column names

I have two tables where I want to join them and do a look up from one to find the column heading in another.
One table looks like this:
table: student_score
student| red |blue |green
------- -------- ------- -----
201 | 88 |89 |78
345 | 67 |72 |95
987 | 75 |81 |89
The other is like this:
table: student_history
student | color_last_year
------- -----------------
201 | red
345 | blue
987 | green
I'm looking to create a query in PostgreSQL that will allow me to pick last year's color (from the history table) as the column heading from the score table. In the past I've used javascript to do this, but would prefer to do it all in one psql query.
The js looked something like this:
function lastYear(color){
var query = 'SELECT student_score.' + color + '
FROM student_score
JOIN student_score ON student_score.student =
student_history.student
//...more code .... //;'
}
I've been trying to find help around this in documentation and searches, but not sure how best to set up my query.
You can use a case expression:
select
s.student,
case h.color_last_year
when 'red' then s.red
when 'blue' then s.blue
when 'green' then s.green
end as val
from student_score s
join student_history h on s.student = h.student;

Postgres-pgloader-transformation in columns

Loading flat file to postgres table.I need to do few transformations while reading the file and load it.
Like
-->Check for characters, if it is present, default some value. Reg_Exp can be used in oracle. How the functions can be called in below syntax?
-->TO_DATE function from text format
-->Check for Null and defaulting some value
-->Trim functions
-->Only few columns from source file should be loaded
-->Defaulting values, say for instance, source file has only 3 columns. But we need to load 4 columns. One column should be defaulted with some value
LOAD CSV
FROM 'filename'
INTO postgresql://role#host:port/database_name?tablename
TARGET COLUMNS
(
alphanm,alphnumnn,nmrc,dte
)
WITH truncate,
skip header = 0,
fields optionally enclosed by '"',
fields escaped by double-quote,
fields terminated by '|',
batch rows = 100,
batch size = 1MB,
batch concurrency = 64
SET work_mem to '32 MB', maintenance_work_mem to '64 MB';
Kindly help me, how this can be accomplished used pgloader?
Thanks
Here's a self-contained test case for pgloader that reproduces your use-case, as best as I could understand it:
/*
Sorry pgloader version "3.3.2" compiled with SBCL 1.2.8-1.el7 Doing kind
of POC, to implement in real time work. Sample data from file:
raj|B|0.5|20170101|ABCD Need to load only first,second,third and fourth
column; Table has three column, third column should be defaulted with some
value. Table structure: A B C-numeric D-date E-(Need to add default value)
*/
LOAD CSV
FROM inline
(
alphanm,
alphnumnn,
nmrc,
dte [date format 'YYYYMMDD'],
other
)
INTO postgresql:///pgloader?so.raja
(
alphanm,
alphnumnn,
nmrc,
dte,
col text using "constant value"
)
WITH truncate,
fields optionally enclosed by '"',
fields escaped by double-quote,
fields terminated by '|'
SET work_mem to '12MB',
standard_conforming_strings to 'on'
BEFORE LOAD DO
$$ drop table if exists so.raja; $$,
$$ create table so.raja (
alphanm text,
alphnumnn text,
nmrc numeric,
dte date,
col text
);
$$;
raj|B|0.5|20170101|ABCD
Now here's the extract from running the pgloader command:
$ pgloader 41287414.load
2017-08-15T12:35:10.258000+02:00 LOG Main logs in '/private/tmp/pgloader/pgloader.log'
2017-08-15T12:35:10.261000+02:00 LOG Data errors in '/private/tmp/pgloader/'
2017-08-15T12:35:10.261000+02:00 LOG Parsing commands from file #P"/Users/dim/dev/temp/pgloader-issues/stackoverflow/41287414.load"
2017-08-15T12:35:10.422000+02:00 LOG report summary reset
table name read imported errors total time
----------------------- --------- --------- --------- --------------
fetch 0 0 0 0.007s
before load 2 2 0 0.016s
----------------------- --------- --------- --------- --------------
so.raja 1 1 0 0.019s
----------------------- --------- --------- --------- --------------
Files Processed 1 1 0 0.021s
COPY Threads Completion 2 2 0 0.038s
----------------------- --------- --------- --------- --------------
Total import time 1 1 0 0.426s
And here's the content of the target table when the command is done:
$ psql -q -d pgloader -c 'table so.raja'
alphanm │ alphnumnn │ nmrc │ dte │ col
═════════╪═══════════╪══════╪════════════╪════════════════
raj │ B │ 0.5 │ 2017-01-01 │ constant value
(1 row)