Use Paste Instead Of Sed

When joining multiple lines of text into a single line of delimiter-separated fields, the line length can silently violate Shell buffer sizes. The construct:

 echo `grep -l foo *`
causes Shell to strip the newlines from the subshell executed within the grave accents, replacing them with spaces; echo posts the result to StandardOutput. One can then use sed to transform the spaces into a delimiter of choice:
 echo `grep -l foo *` | sed 's/ /:/g'
However, this can lead to lines of unbounded length, which can break Shell buffer limitations. The command will create truncated output when the buffer exceeds the line buffer length in sed(1).

Therefore:

when joining multiple lines of text into a single line with a one character separator, the paste(1) program should be used instead of sed(1) if the length of the line is unbounded. Use:

 grep -l foo * | paste -sd':' -
to avoid truncated lines.


CategoryUnixShellPattern


EditText of this page (last edited October 3, 2003) or FindPage with title or text search