I would like to go from the standard find
output which is:
path/to/file1.yaml
path/to/file2.yaml
path/to/file3.yaml
To this:
path/to/file1.yaml,path/to/file2.yaml,path/to/file3.yaml
What's the simplest way to do that from the command line?
I have tried these things:
find . -path '*.yaml' | sed -e 's/\s/,/g'
find . -path '*.yaml' -print0 | sed -e 's/ /,/g'
but doesn't seem to work.
Here is one approach for separating the filenames with commas:
find . -path '*.yaml' | tr '\n' ','
If the file names do not contain white space, then another approach is:
IFS=, echo $(find . -path '*.yaml')
In the comments, Kojiro suggests a third approach which preserves whitespace:
find . -path '*.yaml' -print0 | tr '\0' ,
Because newlines and commas are allowed in file names, this format may lead to confusion. This format should only be used if you know that your files are sensibly named.
This will work too :
find . -path '*.yaml' | paste -s -d ',' -
both python and perl can do it in one line:
python:
find . -path '*.yaml' | python -c 'import sys; print ",".join(sys.stdin.readlines())'
perl:
find . -path '*.yaml' | perl -e 'print join ",", map {chomp;$_} <STDIN>'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With