Ron Ron - 1 month ago 12
Linux Question

How to reduce lines with pipe in linux command line?

Basically I just want to combine all lines into a single line in pipe.

e.g.,

# ls / | grep b
bin
boot
lib

# ls / | grep b | reduce
binbootlib

Answer

Parsing ls output is not recommended as its brittle.

But as for your question you can do something like this:

$ls -1 / | grep b | paste -s -d ''
binbootlib

ls -1 ensures single column format and paste combines all lines of text serially (-s) with delimiter (-d) as nothing ''