0

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

  • So the table name is on the same line where the word you look up is, and it always appears before the word you look up? Can you provide an example in your OP? – cesarv Oct 19 '17 at 15:27
0

Taking just the actual question posed:

[How can one] recursively till it find 'table' and then return the line +3 lines up and down.

This is helpfully provided for with grep, which has three options (one of which implies the two others:

  • -Ax (or "After") shows the x lines after a pattern match (e. g. -A3).
  • -Bx (or "Before") shows the x lines before a pattern match (e. g. -B3).
  • -Cx (or "Context") shows the x lines before and after a pattern match (e. g. -C3). -Cx is identical to -Ax -Bx.

So, to search the file haystack for the three lines before and after every needle:

$ grep -C3 'needle' /path/to/haystack

Or, since the needle in your case is the string table:

$ grep -C3 'table' /path/to/haystack

But if table isn't known to always be lowercase (as mysqldump usually capitalizes keywords), you might want to do a case-insensitive search:

$ grep -i -C3 'table' /path/to/haystack
0

Without a best answer I take my aproximation:

var=$(tr , '\n' < database.sql | grep -m 1 -ne 'str' | cut -f1 -d:) ; head -n$var log.log | grep -m 1 'INSERT INTO'

I prefer one that can start in line x and go up till it find the str, but it's working.

0

Grep has two nice options, -A (after) and -B (before) so:

tr , '\n' < database.sql | grep -ne -A3 -B3 'str'

would return the line containing the string you searched for, plus the three lines above it and the three lines below it.

I'd like to answer the entire question, however I'm afraid I don't really understand what you're trying to do.

To cut the string you could use the command cut and/or awk

AWK is more complex, but a simple use would be awk {'print $1'}

so :

tr , '\n' < database.sql | grep -ne -A3 -B3 'str' | awk {'print $1'} would print the first filed in the result, where field is anything to the first white space.

and similarly with cut:

tr , '\n' < database.sql | grep -ne -A3 -B3 'str' | cut -f1 -d" "

only this time -d specifies the delimiter between fields as white space. It could be anything you want, but only one character. so -f = field, -d = delimiter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.