bash - Awk With Input File Match and Pattern Search -
sorry have never asked question on board such this, please excuse inexperience.
i trying take field input file, field 2 abc.txt, , match in def.txt. problem need match additional pattern in def.txt file.
for exapmle, field 2 in abc.txt "3". , pattern want search in def.txt "efg". need return lines match pattern "efg" , contain "3".
as additional constraint want stop searching after reaches value, "end". have exhausted efforts find simple 1 liner in awk or variant.
i befuddled on of these points, ok ask on novice? assistance appreciated, thanks.
here code, not working @ all: awk 'begin { fs = " " } ;nr==fnr{a[$2]=++i;next} '{if ( $5 in a) && ($0 ~ '/efg/')} {print $0}' abc.txt def.txt
i trying achieve 3 things:
match input file field def.txt fields
match pattern in def.txt
stop search when value encountered, example "end".
hoping 1 line solution if possible, of awk beginner.
sample input abc.txt 1 2 3 4 def.txt 1 abc 1 efg 1 efg more data end 2 ghi 2 efg 2 efg more data end 3 jkl 3 efg 3 efg more data end and on...
expected output 1 efg 1 efg more data 2 efg 2 efg more data 3 efg 3 efg more data and have stop upon reaching "end." instead of going through entire file , printing subsequent instances of 1 efg, 2 efg, etc.
there obvious concerns existing code. provided:
awk 'begin { fs = " " } ;nr==fnr{a[$2]=++i;next} '{if ( $5 in a) && ($0 ~ '/efg'/)} {print $0}' abc.txt def.txt i see you're going this. think mean is:
awk ' # step through first file, recording $2 in array... nr==fnr { a[$2]; next; } # hard stop if signal... $0 == "end" { quit; } # in second+ file, test condition. $5 in && /efg/ ' abc.txt def.txt you can of course compress 1 liner removing comments , newlines:
awk 'nr==fnr{a[$2];next} $0=="end"{quit} $5 in && /efg/' abc.txt def.txt notable changes:
- the single quotes need wrap entire script. 1 @ start, 1 @ end, none "inside".
- awk splits whitespace default, fs may unnecessary (unless you've got tabs in fields, in case may put fs back).
- you don't need increment counter. in awk, if mention array element, "created" without having content, can use conditions
$5 in awithout wasting memory. - the
ifstatement removed. awk takescondition { statement }patterns. condition condition whether it's in format or insideif. - the second element of condition shrunk regex. default, awk take mean "does regex apply current input line".
- the
print $0command removed, because default behaviour if no statement provided.
Comments
Post a Comment