How to concatenate multiple lines of output to one line?
Solution 1
Use tr '\n' ' '
to translate all newline characters to spaces:
$ grep pattern file | tr '\n' ' '
Note: grep
reads files, cat
concatenates files. Don't cat file | grep
!
Edit:
tr
can only handle single character translations. You could use awk
to change the output record separator like:
$ grep pattern file | awk '{print}' ORS='" '
This would transform:
one
two
three
to:
one" two" three"
Solution 2
Piping output to xargs
will concatenate each line of output to a single line with spaces:
grep pattern file | xargs
Or any command, eg. ls | xargs
. The default limit of xargs
output is ~4096 characters, but can be increased with eg. xargs -s 8192
.
Solution 3
In bash echo
without quotes remove carriage returns, tabs and multiple spaces
echo $(cat file)
Solution 4
This could be what you want
cat file | grep pattern | paste -sd' '
As to your edit, I'm not sure what it means, perhaps this?
cat file | grep pattern | paste -sd'~' | sed -e 's/~/" "/g'
(this assumes that ~
does not occur in file
)
Solution 5
This is an example which produces output separate by commas. You can replace the comma by whatever separator you need.
cat <<EOD | xargs | sed 's/ /,/g'
> 1
> 2
> 3
> 4
> 5
> EOD
produces:
1,2,3,4,5
Related videos on Youtube
T. Webster
Updated on September 06, 2021Comments
-
T. Webster over 2 years
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.-
ruakh about 11 yearsBy the way: (1) you need to put your
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.) -
Shervin Emami about 7 yearssimilar to "stackoverflow.com/questions/2764051/…"
-
glerYbo over 5 yearsQuestion with 68 votes (140k views) duplicated with post which has only 1 vote (12k views)? This isn't right.
-
glerYbo over 5 years
-
-
Adarsha about 9 years
| tr '\n' ' '
was not working for me when called throughphp exec
function. It was ignoring tr, and just giving last match from grep.| xargs
worked. -
Rene over 8 yearsThis solution also has the advantage that it 'eats' spaces from the input. +1
-
sehe over 8 years@Stephan there was no need to assume that
cat file
will actually becat
, or even a file. (I just left that part unchanged as it was irrelevant to the question) -
sorin about 8 yearsYou endup with an undesired space at the end with this approach.
-
nexayq over 7 yearsYou need to add
echo ""
at the end to add new line before prompt text. -
Dangercrow over 7 yearsSeverely underrated answer here, this is really simple and works like a charm, with a trailing newline as well.
-
oink over 7 yearsThis works out nicely, however I don't want the separator to show up on the very last entry. Example, per your
"
, it shows up likeone" two" three"
, however I'd want it to show up likeone" two" three
. Note the last three doesn't have a parenthesis. Any ideas? -
Chris Seymour over 7 years@oink you could pipe the results into
sed '$s/..$//'
to delete the last 2 characters on the last line. -
aggsol over 7 yearsThis is especially neat if you use
IFS="$(printf '\n\t')"
-
Normadize about 7 yearsOr just
echo $(<file)
... using echo is not just neater than usingtr '\n' ' '
, but also better (think\r\n
line endings). @aggsol you can just sayIFS=$'\n\t'
-
Elijah Lynn over 5 yearsIf you want to replace newlines with nothing, you need to use the
--delete
option as the default expects two arguments. e.g.tr --delete '\n'
. -
DimiDak over 4 yearsHow about without space?
-
stark almost 4 yearsYou don't need
{print}
since its the default action of awk. -
Mickael B. almost 4 yearsThis as already been answer in 2015. You are just duplicating this answer. Please read answers before posting your own, specially when the question is 7 years old and there are already 8 answers.
-
Chris over 2 yearsWorks very nicely on Unix systems - but Windows (eg. MSYS2 / Git Bash) doesn't behave the same way - could be other systems that don't work the same way as well. awk works most reliably for me cross-platform.
-
psmith about 2 yearsI wanted to use this to grep certain headers from the
curl
response, and apparently headers have a\r
which messed up the output, so you need to delete the\r
chars like this:echo $(curl ... 2>&1 | grep my-headers | tr -d '\r' )
-
dz902 almost 2 yearsThis would remove all double quotes
"
. Not suitable for JSON.