I have a MySQL dump file containing several tables. I already have a SED command to extract one single table.
Now I need to know how to extract only the records associated with a specific unit_id. The format is as follows:
INSERT INTO tablename (1,999,'sometext'), (2,999,'othertext'),(3,997,'text here'),(4,123,'a string'), ...
Where 999 is the unit id (there can be multiple records for a single unit id)
My desired result is:
999,'sometext'
999,'othertext'
...
... for every entry where 999 (or any specific number I choose) appears in the second column.
I tried using sed to select the values between parentheses, like this:
sed -n 's/\((.*,999,.*)\)/\1/p' < result.sql
Where 999 is the id I'm searching for.
but it returns nothing.
Sedcannot output the multiple matching results in the same line at a time. As a workaround we can split the input line at an appropriate punctuation into multiple lines as a preprocessing.Would you try the following:
Output:
sedcommands/), *(/)\'$'\n''(/g'finds a comma between a closing paren and an opening paren then replace it with a newline. (It assumes you are runningbash).sedcommand's/.*,\(999,.*\)).*/\1/p'extracts your desired values.Alternatively
grepwith-Poption (perl-compatible regex support) will also work.(?<=,)is a zero-width lookbehind assertion which matches a leading comma without including it in the result..*?avoids thegreedymatch.(?=\))is a zero-width lookahead assertion which matches a trailing right paren without including it in the result.