简体   繁体   English

字符串替换文件内容

[英]String substitution with file contents

I have a file called tags.txt which contains a tag on each line, for example: 我有一个名为tags.txt的文件,每行包含一个标记,例如:

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. 使用“高级Bash脚本编写指南”,我编写了一个Bash脚本,它接受这些行并将它们组成一个以逗号分隔的单行列表。

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. 每当我尝试将(<tags.txt)替换为第二行中的tagsraw时,有多种形式,我会收到“错误替换”错误。 Is doing this on one line, without an intermediary variable, in Bash even possible? 在Bash中,即使可能,也可以在没有中间变量的情况下执行此操作吗?

You can use awk : 你可以使用awk

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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