How to “merge” patterns in a single line

grepregular expressionsed

I am doing grep and sed and I get 2 lines of a file that I am interested in. How can I get these lines in a single line ending with the new line character?
Now I am getting:

pattern1  
pattern2  

I would like to get pattern1 pattern2 \n

Best Answer

paste:

{...pipeline...} | paste -d " " - -

That says: "read a line from stdin (the first -), read another line from stdin (the second -), then join them with a space"


a bash-specific technique:

$ x=$(grep -o pattern. test.txt)
$ echo "$x"
pattern1
pattern2
$ mapfile -t <<< "$x"
$ echo "${MAPFILE[*]}"
pattern1 pattern2

ref: http://www.gnu.org/software/bash/manual/bashref.html#index-mapfile