index.js
我有一个名为200 多行的文件:
AcademicCapIcon.svelte
AdjustmentsIcon.svelte
AnnotationIcon.svelte
ArchiveIcon.svelte
...
我想将其更改为以下内容:
import AcademicCapIcon from './dir1/dir2/AcademicCapIcon.svelte'
import AdjustmentsIcon from './dir1/dir2/AdjustmentsIcon.svelte'
import AnnotationIcon from './dir1/dir2/AnnotationIcon.svelte'
import ArchiveIcon from './dir1/dir2/ArchiveIcon.svelte'
...
有什么方法可以通过一个衬垫或 shell/bash 脚本从终端完成此操作吗?
我确实喜欢以下内容,但我被困住了:
sed -i -e 's/^/import /' index.js && sed -n 's/$/ from && bla bla.
答案1
$ sed "s:\(.*\)\.svelte:import \1 from './dir1/dir2/&':" index.js
import AcademicCapIcon from './dir1/dir2/AcademicCapIcon.svelte'
import AdjustmentsIcon from './dir1/dir2/AdjustmentsIcon.svelte'
import AnnotationIcon from './dir1/dir2/AnnotationIcon.svelte'
import ArchiveIcon from './dir1/dir2/ArchiveIcon.svelte'
...
如果对结果满意,请添加回-i
答案2
使用 GNU awk:
$ awk -i inplace -v q="'" '{ split($0, arr, "."); print "import " arr[1] " from " q "./dir1/dir2/" $0 q }' index.js
import AcademicCapIcon from './dir1/dir2/AcademicCapIcon.svelte'
import AdjustmentsIcon from './dir1/dir2/AdjustmentsIcon.svelte'
import AnnotationIcon from './dir1/dir2/AnnotationIcon.svelte'
import ArchiveIcon from './dir1/dir2/ArchiveIcon.svelte'