subakhan k subakhan k - 2 months ago 21
Linux Question

How to print all lines until newline after given word matched

I want to print all the lines until newline after given word matched

File:

[`odbcname`]
Driver = /`usr`/lib/`odbc`/`libmyodbc.so`
Description = `MyODBC`3.51 Driver `DSN`
SERVER = my.database.server.com
PORT =

[`MSSQLTestServer`]
Driver = `FreeTDS`


Output:

[`odbcname`]
Driver = /`usr`/lib/`odbc`/`libmyodbc.so`
Description = `MyODBC` 3.51 Driver `DSN`
SERVER = my.database.server.com
PORT =


I tried

cat grep '^\[odbcname]|^$'`


but i don't know how to print all lines until empty space.

Answer
$ awk -v RS= '/\[odbcname\]/' file

[odbcname]
Driver = /usr/lib/odbc/libmyodbc.so
Description = MyODBC3.51 Driver DSN
SERVER = my.database.server.com
PORT =

set record separator to paragraph mode (i.e. one or more blank lines), find the the pattern (and print).