I want to find a paragraph is a list of files. For example an SQL statement which looks like:

upadte b_table
set number = 100014
where id in (
            select number_desc from t_table
            where id > 100);

How should I ignore enter/new lines?

I need to find/print a paragraph which starts with "update" and finishes with the first occurrence of ;

link|improve this question
feedback

3 Answers

Inspired by barryj's concise awk solution:

perl -ne 'print if /update/../;/' *.sql

If the update statements are contained in (empty-line delimited) paragraphs:

perl -00 -ne 'print if /update.*?;/' *.sql

-- from http://uselessuseofcat.com/?p=381

Otherwise, this prints filenames and update statements:

perl -ne 'BEGIN{undef $/}; print "$ARGV\t$.\t$1\n" if m/(update.*?;)/mg' *.sql

-- from http://www.commandlinefu.com/commands/view/5087/multi-line-grep

(untested - caveat emptor)

link|improve this answer
ok - i did used grep with regexp like this: – user84129 Jun 7 '11 at 5:05
feedback

grep is a bit tricky with newlines, but you could try something with awk:

awk '/update/,/;/' < filename.sql
link|improve this answer
How would you deal with a file containing multiple update statements? – RedGrittyBrick Jun 2 '11 at 12:36
@RedGrittyBrick - The above statement should print all statements with the matching start and stop parts. I assume that's what is required. – barryj Jun 2 '11 at 12:55
feedback

I used grep like this:

grep -ihRP  '^(update|( )* update) .* [^;]*;'
link|improve this answer
using grep with perl engine other wise regexp not works "good" in bash and prints everything except ; after the word "update" or " update" until first appearance of ; – user84129 Jun 7 '11 at 5:20
feedback

Your Answer

 
or
required, but never shown

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