Read Table in file and split

HELLO
I need your help please , i need to read a file that contain a table like :

Name  |   Status
------------------
DB 1   |  UP
DB 2  |  UP
DB 3  |  DOWN
DB 4  |  UP
DB 5  |  UP

the objective to read each line and check if DB is UP or Down and give me the name of Down database.
Thank you

DB 3 is down.

3 Likes

Hi,

What have you tried?

This seems to work;

[root@fbakirpomd4 ~]# cat table.txt
Name  |   Status
------------------
DB 1   |  UP
DB 2  |  UP
DB 3  |  DOWN
DB 4  |  UP
DB 5  |  UP

You can put your own wrappers round it.

[root@fbakirpomd4 ~]# awk -F"|" '/DOWN/ { print $1" is "$2 }' < table.txt
DB 3   is   DOWN
[root@fbakirpomd4 ~]#

Regards

Gull04

sed -n '/DOWN/s/|/is/p' table.txt
DB 3  is  DOWN

---

awk -F\| '/DOWN/{print $1}' table.txt
DB 3  
awk '/DOWN *$/ && $(NF-1)="is"' table.txt

Caveat on using /DOWN/ for line match, it returns lines like:

DOWNEY   |  UP
SHOWDOWN  |  UP

Thank you for your help , i have created this one but it show all the file content ...

awk -F"|" ' {
        if ($2!="UP"){
           print "Node" $1 "is" $2 ;
        }
        else {
           print "All Nodes OK" ;
}
}' db_status.txt  | awk '/-------/{flag=1;next}/rows/{flag=0}flag'

thank you

awk -F"|" '
NR > 2 {
   if ($2 !~ /UP/) {
      print "Node: " $1 "is" $2 ;
      all_nodes_ok=1;
   }
}
END {
   if (! all_nodes_ok) print "All Nodes OK" ;
}' db_status.txt

you'll have work on your logic (I'll leave it as an exercise for you), but your modified attempt is:

awk -F'|' 'FNR>1 && NF==2{
        if ($2 ~ "^ *UP *$")
           print "Node" $1 "is" $2 ;
        else
           print "All Nodes OK" ;
}' db_status.txt

thank you rdrtx1,
the first if is working fine, but the second is not working (if all databases is UP the program not printing anything)

Can you post a sample input file for the second?

Updated sample runs with updated second script:

cat << NODES > db_status.txt
Name  |   Status
------------------
DB 1   |  UP
DB 2  |  UP
DB 3  |  UP
DB 4  |  UP
DB 5  |  UP
NODES

awk '
/^Name/ || /^ *--* *$/ {next}
/..*[|]..*/ {
   if ($NF !~ /[Uu][Pp]/) {
      node=$0;
      sub(" *[|].*", "", node);
      print "Node: " node " is " $NF;
      bad_node=1;
   }
}
END {
   if (! bad_node) print "All Nodes OK";
}' db_status.txt

output:

All Nodes OK

next run ex.:

cat << NODES > db_status.txt
Name  |   Status
------------------
DB 1   |  UP
DB 2  |  UP
DB 3  |  DOWN
DB 4  |  UP
DB 5  |  UP
NODES

awk '
/^Name/ || /^ *--* *$/ {next}
/..*[|]..*/ {
   if ($NF !~ /[Uu][Pp]/) {
      node=$0;
      sub(" *[|].*", "", node);
      print "Node: " node " is " $NF;
      bad_node=1;
   }
}
END {
   if (! bad_node) print "All Nodes OK";
}' db_status.txt

output:

Node: DB 3 is DOWN
1 Like
awk -F"|" ' {
        if ( ! /UP/){
           print "Node" $1 "is" $2 ;
        }
        else {
           print "All Nodes OK" ;
}
}' db_status.txt  | awk '/-------/{flag=1;next}/rows/{flag=0}flag'

Thas give me a correct answer