Find 2 expressions then print in a single line

Guys, need help.

I have a file that contains something like this:

abc
def
ghi
jkl

I want to print the first and last line of the file and the output should be in a single line.

so, output should be like this:

abc jkl

try:

awk 'NR==1 {printf $0" "}END {print}' infile
1 Like

It worked!

Moreover, can I add a pattern on that code?
e.g
3 files

abc
def
ghi
jkl
123
456
789
jkl
abc
def
ghi
123

Can I print only the files that contains 'jkl' on the end?

output:

abc jkl
123 jkl

Thanks for the quick response

try also:

awk '$NF ~ /jkl/ {print $1,$NF}' RS= file1 file2 file3