Get the list with sed or awk

#cat file
[General]
aa
bb
cc
[Sabino B]
dd
ee
[XXX C]
[QQQ D]
[zzz D]
77
dd
[FAFS EE]
gg
xx

I want to get: when VAR="zzz D" get below out put

aa
bb
cc
77
dd

the rules is add General items to zzz D item. and get the list

Try

awk '/^\[/{a=""}
a{print}
/\[General\]/{a=1}
/\[zzz D\]/{a=1}' file

Thanks, it works,can you explain it to me

Lei

Minimized by one more line...:smiley:

awk '/^\[/{a=""}                          # If line starts with "[" (means VAR) set a="" (null)
a{print}                                  # If a is present then print
/\[General\]|\[zzz D\]/{a=1}' file      # If line contains [General] or [zzz D]  then set a=1, So that we can print all the lines after these values.

I hope this helps:)

pamu

I understand it partly, if change zzz D to a variable, can we get it eg: VAR="zzz D"

Through Sed..

$ uname -rs
SunOS 5.10
$ sed -n 'H;${
x
s/[^]]*]\n\([^[]*\).*\[zzz D\]\n\([^[]*\)\n.*/\1\2/p
}' inputfile
aa
bb
cc
77
dd
$
awk '/^\[/{a=""}
a{print}
$0~"[General]" || $0~var {a=1}' var="[zzz D]" file

Edit: This gives to much, not sure whats wrong

I am not clear what you are saying.. :confused:

As per your requirement code prints lines after if it finds [General] or [zzz D].

Here i just use a for printing the expected result.

This will fail if it file starts with any other tags than [General].

And if you are saying as you want to pass variable as Jotne pointed out then use.

awk -v var="[zzz D]" '/^\[/{a=""}
a{print}
/\[General\]/ || $0 == var{a=1}' file 

@Jotne -
you can do like /\[General\]/ || $0 == var