When I want to print lines between two patterns, excluding the lines with those patterns, I can do it in ex
using +1
and -1
after the pattern. That is awesome. Is this possible in awk
? Right now, I kludge it by setting an is_printing
flag.
This is ex
to get the column definitions from an SQL table creation:
$ ex schema/media.sql <<< '/^CREATE TABLE/+1,/^)/-1p'
# id
# SMALLINT
# UNSIGNED
# NOT NULL
# AUTO_INCREMENT
# COMMENT 'The auto-generated ID.',
# parent_id
# SMALLINT
# UNSIGNED
# NULL
# COMMENT 'The ID of the parent album, if any.',
# title
# VARCHAR(255)
# COLLATE utf8_unicode_ci
# NOT NULL
# DEFAULT ''
# COMMENT 'The album title.',
# description
# TEXT
# COLLATE utf8_unicode_ci
# NOT NULL
# COMMENT 'The album''s description.',
This is an awk
command to do the same:
$ awk '/^\)/ { exit; } is_printing; /^CREATE TABLE/ { is_printing = 1; }' schema/media.sql
I find the awk version not as readable. Is there an idiom I am missing? Can sed
do something like this, too? (I prefer awk
's syntax over sed
's.)