How to concatenate multiple lines of output to one line?

324,257

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
Share:
324,257

Related videos on Youtube

T. Webster
Author by

T. Webster

Updated on September 06, 2021

Comments

  • T. Webster
    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
      ruakh about 11 years
      By the way: (1) you need to put your sed script in single-quotes so that Bash doesn't mess with it (since sed s/\n/ /g calls sed with two arguments, namely s/n/ and /g); (2) since you want the output of cat file | grep pattern to be the input to sed, not the arguments to sed, you need to eliminate xargs; and (3) there's no need for cat here, since grep can take a filename as its second argument. So, you should have tried grep 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
      Shervin Emami about 7 years
    • glerYbo
      glerYbo over 5 years
      Question with 68 votes (140k views) duplicated with post which has only 1 vote (12k views)? This isn't right.
    • glerYbo
      glerYbo over 5 years
  • Adarsha
    Adarsha about 9 years
    | tr '\n' ' ' was not working for me when called through php exec function. It was ignoring tr, and just giving last match from grep. | xargs worked.
  • Rene
    Rene over 8 years
    This solution also has the advantage that it 'eats' spaces from the input. +1
  • sehe
    sehe over 8 years
    @Stephan there was no need to assume that cat file will actually be cat, or even a file. (I just left that part unchanged as it was irrelevant to the question)
  • sorin
    sorin about 8 years
    You endup with an undesired space at the end with this approach.
  • nexayq
    nexayq over 7 years
    You need to add echo "" at the end to add new line before prompt text.
  • Dangercrow
    Dangercrow over 7 years
    Severely underrated answer here, this is really simple and works like a charm, with a trailing newline as well.
  • oink
    oink over 7 years
    This works out nicely, however I don't want the separator to show up on the very last entry. Example, per your ", it shows up like one" two" three", however I'd want it to show up like one" two" three. Note the last three doesn't have a parenthesis. Any ideas?
  • Chris Seymour
    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
    aggsol over 7 years
    This is especially neat if you use IFS="$(printf '\n\t')"
  • Normadize
    Normadize about 7 years
    Or just echo $(<file) ... using echo is not just neater than using tr '\n' ' ', but also better (think \r\n line endings). @aggsol you can just say IFS=$'\n\t'
  • Elijah Lynn
    Elijah Lynn over 5 years
    If 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
    DimiDak over 4 years
    How about without space?
  • stark
    stark almost 4 years
    You don't need {print} since its the default action of awk.
  • Mickael B.
    Mickael B. almost 4 years
    This 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
    Chris over 2 years
    Works 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
    psmith about 2 years
    I 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
    dz902 almost 2 years
    This would remove all double quotes ". Not suitable for JSON.