在以“declarations:”字符串开头的行的文本文件中,将“declarations”替换为“imports”,将“AppComponent”或“NxWelcomeComponent”替换为“”,解析结果字符串以获取用逗号“,”分隔的单个字符串,并从上面获取它们相关的导入字符串。例如:
import { RouterModule } from '@angular/router';
import { ScrollDirective } from './directives/scroll.directive';
import { TestComponent } from './test';
import { Test2 } from './test2';
import { Test3 } from './test';
@NgModule({
declarations: [AppComponent, NxWelcomeComponent, ScrollDirective, TestComponent, Test2],
})
预期输出:
import { ScrollDirective } from './directives/scroll.directive';
import { TestComponent } from './test';
import { Test2 } from './test2';
imports: [ScrollDirective, TestComponent, Test2],
我试过了:test.awk:
/declarations:/ {str=$0; sub("declarations", "imports", str) ; sub("AppComponent,", "", str) ; sub("NxWelcomeComponent,", "", str) }
/import/ { importbuf[++arrindex]=$0}
END {
for (i in importbuf) {
print importbuf[i]
}
print str
}
awk -f test.awk in
额外输入2:
import { RouterModule } from '@angular/router';
import { ScrollDirective } from './directives/scroll.directive';
import { TestComponent } from './test';
import {Test4} from './test4';
@NgModule({
declarations: [AppComponent, NxWelcomeComponent, ScrollDirective, TestComponent, Test4],
})
预期结果2:
import { ScrollDirective } from './directives/scroll.directive';
import { TestComponent } from './test';
import {Test4} from './test4';
imports: [ScrollDirective, TestComponent, Test4],
awk -f test.awk in2
您可以使用这个 2 pass awk 解决方案。
然后使用它作为:
或者对于第二个例子:
或者,您可以将 Ruby 与正则表达式一起使用,并对正则表达式中的数组设置算术。
以你两个例子为例,尝试
file_1
一下file_2
:然后运行该程序:
印刷:
或者你的第二个例子:
印刷: