subakhan k subakhan k - 2 months ago 7
Linux Question

How to get expected string in linux shell script

I need to delete all line except the string that are with in the character "[ ]".

Input file: ODBC.ini

[ODBC Data Sources]

odbcname = MyODBC 3.51 Driver DSN
[odbcname]

Driver = /usr/lib/odbc/libmyodbc.so
Description = MyODBC 3.51 Driver DSN
[Default]


Expected Output : ODBC.ini

[ODBC Data Sources]

[odbcname]

[Default]


Also need to different dsn names after deleting this old one.
Thanks in Advance

Answer

You can use awk. The following awk program matches either lines containing a [value in brackets] or empty lines.

awk '/^\[.*]$/ || /^$/' file

Output:

[ODBC Data Sources]

[odbcname]

[Default]
Comments