user3241815
user3241815

Reputation: 1

How to search the content of a file using sed

I tried several commands and read a few tutorials, but none helped. I'm using gnu sed 4.2.2. I just want to search in a file for:

func1(int a)
{

I don't know if it is the fact of a newline followed by "{" but it just doesn't work. A correct command would help and with an explanation even more. Thanks!

Note: After "int a)" I typed enter, but I think stackoverflow doesn't put a newline. I don't want confusion, I just want to search func1(int a)'newline'{.

Upvotes: 0

Views: 379

Answers (1)

Jakub Kotowski
Jakub Kotowski

Reputation: 7571

sed -n '/func1/{N;/func1(int a)\n{/p}'

Explanation:

sed -n '/func1/{                         # look for a line with "func1"
                N                        # append the next line
                /func1(int a)\n{/p       # try to match the whole pattern "func1(int a)\n{"
                                         # and print if matched
               }'                        

With grep it would be for example:

grep -Pzo 'func1\(int a\)\n{'

but notice that thanks to -z the input to grep will be one large "line" that includes the newline characters too (that is unless the input contains null characters). Parenthesis have to be escaped in this case because it is a Perl regular expression (-P). -o makes grep print just the matched pattern.

Upvotes: 3

Related Questions