Skip to content

Instantly share code, notes, and snippets.

@JPvRiel
Last active August 19, 2024 23:54
Show Gist options
  • Save JPvRiel/b337dfee8f273aac1332447ed1342304 to your computer and use it in GitHub Desktop.
Save JPvRiel/b337dfee8f273aac1332447ed1342304 to your computer and use it in GitHub Desktop.
Replacing newlines with commas (or other text) using pure bash string substitution (not awk).

In this example

  • Multiline input can be caputured by spreading openening and closing quote accross lines
  • Newlines can be replaced using built-in bash text substitution
  • The main trick is knowing that $'\n' is the way to specify the newling within the subsitution
  • Note, echo output of a var without quotation replaces newlines with spaces
$ t='1
> 2
> 3'
$ echo $t
1 2 3
$ $ echo "$t"
1
2
3
$ echo "${t//$'\n'/','}"
1,2,3

This could be leveraged futher with cat and files

$ for i in {1..3}; do echo $i >> file.txt; done
$ f=$(cat file.txt)
$ echo "$f"
1
2
3
$ echo "${f//$'\n'/','}"
1,2,3

Take note of ${f// instead of just ${f/, becuase you want to subsitute all newlines, not just the first instance of a newline.

Lastly, beware that the above method loads file content into a bash variable (fine for small content). To handle subsitution in very large files, using cat into a var is a bad idea.

Reference (related)

@JPvRiel
Copy link
Author

JPvRiel commented Jul 19, 2021

Note the extra / after 'f'.

Thanks @housni, indeed, need to substitute all, not just the first instance. Corrected the example and expanded it to 3 elements / 2 newlines, so the all vs first mistake is more obvious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment