简体   繁体   中英

String substitution with file contents

I have a file called tags.txt which contains a tag on each line, for example:

workflow
blogging
writing

Using the "Advanced Bash Scripting Guide" I've written a Bash script that takes these lines and makes them into a single-line comma-delimited list.

tagsraw=$(<tags.txt)
tags=${tagsraw//$'\n'/', '}

How can I do this in one line of code?

Whenever I have tried putting the (<tags.txt) in place of tagsraw in the second line, in numerous forms, I get a "bad substitution" error. Is doing this on one line, without an intermediary variable, in Bash even possible?

You can use awk :

tags="$(awk -v OFS=', ' -v RS= '{$1=$1}1' tags.txt)"
echo "$tags"
workflow, blogging, writing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM