Reputation: 8796
I have a grep ouput which looks as follows:
Path to log files = /data/db2inst1/NODE0000/SQL00001/LOGSTREAM0000/
Database is in write suspend state = YES
Path to log files = /data/db2inst1/NODE0001/SQL00001/LOGSTREAM0001/
Database is in write suspend state = NO
I want to edit this as follows:
Node = NODE0000
Database is in write suspend state = YES
Node = NODE0001
Database is in write suspend state = NO
Is that possible?
Upvotes: 0
Views: 187
Reputation: 13239
Using sed (that can also replace your grep
command):
sed -n '/^Database is in write suspend state/p;
/^Path to log files = /s|.*/.*\(NODE[0-9]\+\).*/$|Node = \1|p' file
Upvotes: 0
Reputation: 36
$ awk -F"/" '{if(NR!=1) if($1 ~ /Path to log files/) print $1,$4 ;else print ;else print "Node = "$4 }' temp
Node = NODE0000
Database is in write suspend state = YES
Path to log files = NODE0001
Database is in write suspend state = NO
Upvotes: 0
Reputation: 85550
You don't need to keep pipelines of grep
to generate the output you are looking for. Awk
is a bit powerful than grep
in a way it allows you to do transformation on the text that you've matched, which is quite powerful. So with that, all you need for your example would be
awk '/Path to log files/{match($0,/NODE([^/]+)/,a); printf "Node = NODE%s\n",a[1];next}
/Database is in write suspend state/' file
Upvotes: 2