Search a file with keywords

Hi All

I have a file of format

[EM]
asdf
asf
first
sec

[US]
endi
asdk
rt

[SKP]
123
ferf
dfg
ijglkp
jhgddd

I need help writing an expression that if the file is searched with US as keyword it should return
endi asdk rt
if SKP is searched it should return
123 ferf dfg ijglkp jhgddd

Thank you for help
Abdul

The first argument to the following script is the keyword (EM, US, SKP ... without brackets). The second argument is an optional filename. If provided, the file's contents are searched. If not provided, standard input is read.

#!/bin/sh

awk 'i{if (!NF) exit; print} /\['"$1"'\]/{i=1}' "${2:--}" | paste -d' ' -s -

Thanks for reply I am trying to get the output in variable like below , but its throwing an error .

mail_list=awk 'i{if (!NF) exit; print} /\['"$1"'\]/{i=1}' "${2:--}" | paste -d' ' -s -
echo $mail_list

Wow ... talk about a blast from the past :wink:

Try:

mail_list=$(awk 'i{if (!NF) exit; print} /\['"$1"'\]/{i=1}' "${2:--}" | paste -d' ' -s -)

If you would like to know more about how that works, read about command substitution @ Shell Command Language

Regards,
Alister

mail_list=$(awk 'i{if (!NF) exit; printf $0 FS} /\['"$1"'\]/{i=1}' "${2:--}" )

Thanks guys

awk -v RS="" -v str="US"  '  $1 ~ str { OFS=" "; $1=""; print } '  file

Try:

sed -n '/EM/,/^$/ {
        s/[^a-z]*//g
        p
        }' file | xargs