I want to find the table that contains one value. I see some solutions using mysql, but I started to think, how can I do this in linux?, and I don't have any Idea, but I want to know. So, the steps would be: - Find a word in a file (mysqldump) - go back to find the table name (more or less the line that cointains the word 'table')
It have an additional problem, because when grep find the word it return a huge line, so you need to cut in some way it.
My nearest aproximation:
tr , '\n' < database.sql | grep -ne 'str'
Then maybe I can do something like get the previously lines with
head but I don't know how to do it recursively till it find 'table' and then return the line +3 lines up and down.
To clarify my question. The file more or less contain:
[....] (line 234) "INSERT INTO table_name ...." [...] (line 400) ... str ... [...]
It whould be:
tr , '\n' < database.sql to chop the long lines
grep -ne 'str' to find the line number, in the example it's 400
then it must start looking in the file from this line upward to find the other word
tac [start in line 400] | grep -m 1 "INSERT INTO"
all in one line
UPDATE I have a good aproximation:
var=$(tr , '\n' < database.sql | grep -m 1 -ne 'str' | cut -f1 -d:) ; head -n$var log.log | grep -m1 -C2 'INSERT INTO'
I prefer one that can start in line x and go up till it find the str, but it's works