从比赛&处理多个输入文件 [英] print from match & process several input files

查看:110
本文介绍了从比赛&处理多个输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您仔细查看过去几周的问题时,您会发现我提出的问题与此类似.因为我真的不知道我的问题来自何处,所以我在以要求的格式提问时遇到了问题. E. Morton告诉我不要使用范围表达式.好吧,我不知道它们到底是什么.我在这个论坛上发现了许多类似我的问题以及有效的答案.

when you scrutiny my questions from the past weeks you find I asked questions similar to this one. I had problems to ask in a demanded format since I did not really know where my problems came from. E. Morton tells me not to use range expression. Well, I do not know what they are excactly. I found in this forum many questions alike mine with working answers.

例如:如何从匹配项中打印以下行"(

Like: "How to print following line from a match" (e.g.)

但是,当我处理多个输入文件时,我发现的所有解决方案都停止工作.我需要处理很多. 我使用以下命令:

But all solutions I found stop working when I process more than one input file. I need to process many. I use this command:

gawk -f 1.awk print*.csv > new.txt

1.awk包含:

    BEGIN { OFS=FS=";"
pattern="row4"
}
go {print} $0 ~ pattern {go = 1}

输入文件1 print1.csv包含:

input file 1 print1.csv contains:

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;;;;;;;

输入文件2 print2.csv包含仅用于说明目的的文件.

Input file 2 print2.csv contains the same just for illustration purpose.

1.awk(以及我在本论坛中找到的其他几种从比赛中打印的方式)可用于一个文件.输出:

The 1.awk (and several others ways I found in this forum to print from match) works for one file. Output:

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;;;;;;;

但是当我处理更多输入文件时不是. 每次我以这种方式处理时,似乎都会忽略多个输入文件awk命令从匹配项打印". 如前所述,我被告知不要使用范围表达式.我不知道怎么做,也许这个问题与我输入多个文件的方式有关?

BUT not when I process more input files. Each time I process this way more than one input file awk commands 'to print from match' seem to be ignored. As said I was told not to use range expression. I do not know how and maybe the problem is linked to the way I input several files?

推荐答案

只需在每个文件的开头重置匹配指示器

just reset your match indicator at the beginning of each file

$ awk 'FNR==1{p=0} p; /row4/{p=1} ' file1 file2

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;;;;;;;
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;;;;;;;

更新

从评论中

是否可以将您的awk与:"If $ 1 =" row5然后写入 $ 6 ="row5"并删除$ 5中的值"row5"?换句话说,要移动 如果在第1列找到内容"row5",则将其添加到新的第6列?我可以 与另一个awk一起使用,但是将其组合在一起会更好

is it possible to combine your awk with: "If $1="row5" then write in $6="row5" and delete the value "row5" in $5? In other words, to move content "row5" in column1, if found there, to new column 6? I could to this with another awk but a combination into one would be nicer

... $1=="row5"{$6=$5; $5=""} ...

,或者,如果要使用其他字段代替$5,请用相应的字段号替换$5.

or, if you want to use another field instead of $5 replace $5 with the corresponding field number.

这篇关于从比赛&处理多个输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆