1

Suppose a file with such content:

a
x
y
z
c
some to be omitted
a
b
c

I want to print all those lines which are between the "a" line and "c" line (both with and without the "c" line are ok):

a
x
y
z
c
a
b
c

I tried the sed command:

sed -n '/a/{:my_tag /./p; N; /c/ t end; t my_tag; :end}'

and it gives me the output:

a
a

Does the N in the command work for once only? I cannot see any loop work out here. the help info of sed is a little bit confusing and it seemed like I was doing in the wrong way.

Or maybe some tools rather than sed would be more helpful and efficient to this problem and please show me how.

Liu Weibo
  • 434
  • 5
  • 16

2 Answers2

2

Could you please try following(if ok with awk).

awk '/^a/{flag=1} flag; /^c/{flag=""}'  Input_file

Output will be as follows.

a
x
y
z
c
a
b
c
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
1

With sed:

sed -n '/^a$/,/^c$/p' file

or

sed '/^a$/,/^c$/!d' file

Output:

a
x
y
z
c
a
b
c
Cyrus
  • 84,225
  • 14
  • 89
  • 153