在以“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