Bash
如何修改文件中的所有行
我有一個
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.
$ 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
使用 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'