From the below extract of data I am trying to view out only the items in bold, is there a way to do this? The specific section of the file of this is important which is why the 'into .db. values' is important - also assume I do NOT know the actual value of these
grep "INTO .db. VALUES" ./anikokiss_mysql4.sql
INSERT INTO db
VALUES ('localhost','blog','yscr_bbYcqN','Y','Y','Y','Y','Y','N','N','N','N','N'),('localhost','blog1','yscr_bbS4kf','Y','Y','Y','Y','Y','N','N','N','N','N'),('localhost','blog','yscr_bbhrSZ','Y','Y','Y','Y','Y','N','N','N','N','N'),('localhost','blog','yscr_bbBl0C','Y','Y','Y','Y','Y','N','N','N','N','N'),('localhost','blog','yscr_bbrsKX','Y','Y','Y','Y','Y','N','N','N','N','N');
Result of
cut -d',' -f2 <(awk '/.*INTO .?db.? VALUES.*/,/.*;/{if (NR > 2) nr[NR]}; NR in nr' ./anikokiss_mysql4.sql)
Is
'blog'
As such it appears to only pick the first batch of command seperated values and ignore those after
Similarly with grep command
grep "INTO .db. VALUES" ./anikokiss_mysql4.sql | cut -d "'" -f4
We get the result
blog
When I was updating DB names
find . -name '*.sql' -exec sed -i "s/\\(CREATE DATABASE [^\`]*\`\\)/\\1${cpuser}_/" {}
Current output
[root@uk01 public_html]# grep -Pzo '(?s)INTO .?db.? VALUES[^(]\K[^;]*' aniko.sql | grep -Pao '\(([^,]*,){2}\K[^,]*' | sed -e 's/^/test_/'
test_'yscr_bbYcqN'
test_'yscr_bbS4kf'
test_'yscr_bbhrSZ'
test_'yscr_bbBl0C'
test_'yscr_bbrsKX'
CodePudding user response:
This uses grep
to achieve what you want:
grep -Pzo '(?s)INTO .?db.? VALUES[^(]\K[^;]*' ./anikokiss_mysql4.sql | grep -Pao '\([^,]*,\K[^,]*'
First I use grep
to get everything after INTO db VALUES
. Then, pass the output of that to another instance of grep
which cuts it after the first ,
.
To change what column to get you can just keep repeating the look behind in the last grep {N}
:
# This gets first column
grep -Pzo '(?s)INTO .?db.? VALUES[^(]\K[^;]*' file | grep -Pao '\(([^,]*,){0}\K[^,]*'
# This gets fourth column
grep -Pzo '(?s)INTO .?db.? VALUES[^(]\K[^;]*' file | grep -Pao '\(([^,]*,){3}\K[^,]*'
Will get the fourth column,