Add and last
to your one-liner like so (extra quotes removed):
perl -ne 'print $1 and last if /KEY1="(.*?)"/' myfile
This works because -n
switch effectively wraps your code in a while
loop. Thus, if the pattern matches, print
is executed, which succeeds and thus causes last
to be executed. This exits the while
loop.
You can also use the more verbose last LINE
, which specifies the (implicit) label of the while
loop that iterates over the input lines. This last form is useful for more complex code than you have here, such as the code involving nested loops.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…