Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get string from two rows

Tags:

grep

sed

awk

perl

Want to convert output to variables, from the first row want to take every time the last result.

There are 1 server(s) in cluster: SQL_GDM
MS_SQL_sql1.local.com---RUNNING
There are 2 server(s) in cluster: MONGO_GDM
MS_MONGO1_mongo1.local.com---RUNNING
MS_MONGO2_mongo2.local.com---RUNNING

Expected view:

SQL_GDM|sql1.local.com|RUNNING
MONGO_GDM|mongo1.local.com|RUNNING
MONGO_GDM|mongo2.local.com|RUNNING

For now I just take only the state of the servers with:

grep -oP '(?<=---)\w+'

I'm wondering how to remove MS_*_ and to stay only the host and after that to grep the state of the server.

like image 840
Kalin Borisov Avatar asked Nov 19 '25 18:11

Kalin Borisov


1 Answers

One way with awk. It uses a regular expression to split fields, one or more hyphens (-+) or a colon followed by optional spaces characters (:[[:space:]]*). That way is easier to find either the cluster and the state. For the server I split with _ and extract last path.

Content of script.awk:

BEGIN {
        FS = "-+|:[[:space:]]*"
}

$0 ~ /:/ {
        cluster = $NF
        next
}

{
        n = split( $1, server, /_/ )
        printf "%s|%s|%s\n", cluster, server[ n ], $NF
}

Run it like:

awk -f script.awk infile

And it yields:

SQL_GDM|sql1.local.com|RUNNING
MONGO_GDM|mongo1.local.com|RUNNING
MONGO_GDM|mongo2.local.com|RUNNING
like image 133
Birei Avatar answered Nov 22 '25 08:11

Birei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!