首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用终端/Bash脚本向目录中的所有文件插入字符串

如何使用终端/Bash脚本向目录中的所有文件插入字符串
EN

Unix & Linux用户
提问于 2022-02-06 07:15:43
回答 1查看 247关注 0票数 0

我有一大堆文件。

代码语言:javascript
运行
复制
AcademicCapIcon.svelte        ArrowSmLeftIcon.svelte        CalculatorIcon.svelte
AdjustmentsIcon.svelte        ArrowSmRightIcon.svelte       CalendarIcon.svelte
...
...

所有文件都有相同的格式。例如,AcademicCapIcon.svelte有以下内容:

代码语言:javascript
运行
复制
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
  <path d="M12 14l9-5-9-5-9 5 9 5z"/>
  <path d="M12 14l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14z"/>
  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 14l9-5-9-5-9 5 9 5zm0 0l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14zm-4 6v-7.5l4-2.222"/>
</svg>

我想在每个文件的开头插入以下内容。

代码语言:javascript
运行
复制
<script>
  export let className = "h-6 w-6";
</script>

并在class={className}后插入xmlns="http://www.w3.org/2000/svg"

例如,上述AcademicCapIcon.svelte文件的最终结果将是:

代码语言:javascript
运行
复制
<script>
  export let className = "h-6 w-6";
</script>

<svg xmlns="http://www.w3.org/2000/svg" class={className} fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
  <path d="M12 14l9-5-9-5-9 5 9 5z"/>
  <path d="M12 14l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14z"/>
  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 14l9-5-9-5-9 5 9 5zm0 0l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14zm-4 6v-7.5l4-2.222"/>
</svg>

如何使用终端或Bash脚本完成此操作?

EN

回答 1

Unix & Linux用户

发布于 2022-02-06 08:33:13

将插入内容放入自己的文件中,然后尝试

代码语言:javascript
运行
复制
sed 's|xmlns="http://www.w3.org/2000/svg"|& class={className}| ' insert_file orig_file
<script>
  export let className = "h-6 w-6";
</script>
<svg xmlns="http://www.w3.org/2000/svg" class={className} fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
.
.
.
</svg>
票数 0
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/689494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档