If I run the command cat file | grep pattern
, I get many lines of output. How do you concatenate all lines into one line, effectively replacing each "\n"
with "\" "
(end with "
followed by space)?
cat file | grep pattern | xargs sed s/\n/ /g
isn't working for me.
sed
script in single-quotes so that Bash doesn't mess with it (sincesed s/\n/ /g
callssed
with two arguments, namelys/n/
and/g
); (2) since you want the output ofcat file | grep pattern
to be the input tosed
, not the arguments tosed
, you need to eliminatexargs
; and (3) there's no need forcat
here, sincegrep
can take a filename as its second argument. So, you should have triedgrep pattern file | sed 's/\n/ /g'
. (In this case it wouldn't have worked, for reasons given at the above link, but now you know for the future.) – Manche