我在stackoverflow上找到了关于从匹配(row4)(link)打印的awk,结合了每次打开新文件时由于重置匹配指示符而处理多个文件的功能。 每次输入file1,file2和file 3都是这样的:
row1;something;in;this;row;;;;;;;
row2;something;in;this;row;;;;;;;
row3;something;in;this;row;;;;;;;
row4;don't;need;to;match;the;whole;line,;
row5;something;in;this;row;;;;;;;
row6;something;in;this;row;;;;;;;
row7;something;in;this;row;;;;;;;
row8;something;in;this;row;;;;;;;
row9;something;in;this;row;;;;;;;
row10;something;in;this;row;;;;;;;
输出应为:
works something in this row
row6 something in this row
row8 something in this row
row9 something in this row
row10 something in this row
works something in this row
row6 something in this row
row8 something in this row
row9 something in this row
row10 something in this row
works something in this row
row6 something in this row
row8 something in this row
row9 something in this row
row10 something in this row
在原始帖子link中,它说只需添加
即可... $1=="row5"{$6=$5; $5=""} ...
如果匹配条件为true,则“移动”字段:
awk 'FNR==1{p=0} p; /row4/{p=1} ' file1 file2
但我必须在哪里添加这个?我尝试了我看到的所有组合,但它没有用。在我的情况下,我还想在匹配“第7行”后删除一行,如果是==“第5行”则更改字段$ 1
if ($1=="row7") {next};
if ($1=="row5") {$1="works"}
在我的情况下,我将规则放入一个文件:test.awk并保存以供以后使用。我以这种方式打电话:
gawk -f test.awk *.csv
(file1.csv,file2.csv和file3.csv)。如果我把它们放在p规则之前,这两个新条件仍然被忽略:
FNR==1 {p=0}
$1=="row5" {$5="works"}
$1=="row7" {next}
p;
/row4/{p=1}
答案 0 :(得分:1)
awk -F';' 'FNR==1 {p=0}
$1=="row5" {$6=$5; $5=""}
$1=="row7" {next}
p;
/row4/ {p=1}' file1 file2
p
是p!=0{print}
的缩写,您要应用的条件应该在打印之前,否则它们将无法显示。