Sample Header Ad - 728x90

Why can't I print this regex range with ed(1)?

6 votes
1 answer
332 views
I'm totally mystified why I can't print this range with ed *when there are two ranges*—see *file2.tf* file—; but I can print when there is only *one range*—see *file1.tf* file—and I can print with the gsed (GNU sed on macOS) command; but I can't print with ed. Please consider my shell session and clarify my misconceptions:
$ ed -s file1.tf <<<',n'
1	# some comment
2	module "hello_world" {
3	  source = "./mydir"
4	}
5	# another comment
$ ed -s file1.tf <<<'/module.*world/,/}/p'
module "hello_world" {
  source = "./mydir"
}
$ ed -s file2.tf <<<',n'
1	# some comment
2	module "hello_world" {
3	  source = "./mydir"
4	}
5	# another comment
6	# some comment
7	module "hello_again" {
8	  source = "./anotherdir"
9	}
10	# another comment
$ ed -s file2.tf <<<'/module.*again/,/}/p'
?
$ gsed -n '/module.*again/,/}/p' file2.tf
module "hello_again" {
  source = "./anotherdir"
}
Update: The reverse direction works; but I'm not sure why:
$ ed -s file2.tf <<<'?module.*again?,?}?p'
module "hello_again" {
  source = "./anotherdir"
}
Update 2: The ?? approach doesn't actually work as intended (if there are *three sections* like in *file3.tf* file example) and see answer for an explanation.
$ ed -s file3.tf <<<,n
1	# some comment
2	module "hello_world" {
3	  source = "./mydir"
4	}
5	# another comment
6	# some comment
7	module "hello_again" {
8	  source = "./anotherdir"
9	}
10	# another comment
11	# some comment
12	module "hello_yet_again" {
13	  source = "./yetanotherdir"
14	}
15	# another comment
$ ed -s file3.tf <<<'?module.*hello_again?,?}?p'
module "hello_again" {
  source = "./anotherdir"
}
# another comment
# some comment
module "hello_yet_again" {
  source = "./yetanotherdir"
}
Asked by mbigras (3472 rep)
Jan 5, 2024, 05:54 AM
Last activity: Jan 5, 2024, 07:23 PM