如果AWK中满足条件,则打印行范围 [英] Print range of rows if a condition is met in AWK

查看:1112
本文介绍了如果AWK中满足条件,则打印行范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是使用awk在满足特定条件的行上方显示2行,在行下方显示2行.例如,我正在搜索字符串's62234',找到后,我想打印蓝色矩形内的所有行,如所附的屏幕快照所示.

What I am trying to do is to show 2 rows above and 2 rows below a line that meets a certain criteria without a pipe, using awk. For example, I am searching for the string 's62234' and when found, I want to print all the rows bounded in the blue rectangle as shown in the attached screenshot.

这是我正在使用的文件(thefmifile.txt)

This is the file I am using (thefmifile.txt)

s62098:x:1271:504:Velizar Vrabchev,SI,3,1:/home/SI/s62098:/bin/bash
s62101:x:1272:504:Georgi Georgiev,SI,3,5:/home/SI/s62101:/bin/bash
s62108:x:1273:504:Sherif Kunch,SI,3,1:/home/SI/s62108:/bin/bash
s62111:x:1274:504:Yulian Bizeranov,SI,3,3:/home/SI/s62111:/bin/bash
s62121:x:1275:504:Daniel Dimitrov,SI,2,1:/home/SI/s62121:/bin/bash
s62133:x:1276:504:Ivaylo Ivanov,SI,2,2:/home/SI/s62133:/bin/bash
s62160:x:1277:504:Veniyana Tsolova,SI,2,3:/home/SI/s62160:/bin/bash
s62199:x:1278:504:Nikola Petrov,SI,2,5:/home/SI/s62199:/bin/bash
s62219:x:1279:504:Viliyan Ivanov,SI,2,6:/home/SI/s62219:/bin/bash
s62234:x:1280:504:Viktoriya Dobreva,SI,2,3:/home/SI/s62234:/bin/bash
s855264:x:1281:504:Toni Dupkarski,SI,4,2:/home/SI/s855264:/bin/bash
s81555:x:1282:503:Elena Georgieva,KN,2,0:/home/KN/s81555:/bin/bash
s81585:x:1283:503:Stela Marinova,KN,2,0:/home/KN/s81585:/bin/bash
s81441:x:1284:503:Vesela Plamenova Borislavova , KN, k2, g7:/home/KN/s81441:/bin/bash
s81644:x:1285:503:Viktor Rusev, KN, k2, g7:/home/KN/s81644:/bin/bash
s81628:x:1286:503:Iliyan Yordanov Yordanov, KN, k2, g6:/home/KN/s81628:/bin/bash
s81490:x:1287:503:Yana Spasova, KN, k2, g6:/home/KN/s81490:/bin/bash

我尝试过的是使用awk查找符合条件的行,并使用NR获取所需的其他行的编号,但似乎我缺少了一些东西.

What I have tried is using awk to find the row that meets the criteria and use NR to get the numbers of the other rows needed, but seems I am missing something.

这是我使用的命令:

cat thefmifile.txt | awk -F ':' '$1==s62234 {for (x = NR -2; x <= NR + 2; x++){print}}'

输出在下面的屏幕截图中.

Output is in the below screenshot.

这是所需的输出:

s62199:x:1278:504:Nikola Petrov,SI,2,5:/home/SI/s62199:/bin/bash
s62219:x:1279:504:Viliyan Ivanov,SI,2,6:/home/SI/s62219:/bin/bash
s62234:x:1280:504:Viktoriya Dobreva,SI,2,3:/home/SI/s62234:/bin/bash
s855264:x:1281:504:Toni Dupkarski,SI,4,2:/home/SI/s855264:/bin/bash
s81555:x:1282:503:Elena Georgieva,KN,2,0:/home/KN/s81555:/bin/bash

当它是{print x}时,它显示了我需要的行号,但是有某种方法可以将文件的行作为数组中的元素来访问,而只是使用此"x"作为索引(例如,某些像NR [x])?

When it is {print x} it shows the numbers of the lines I need, but is there some way to access the lines of the file as elements in array and just to use this 'x' as an index (e.g. something like NR[x])?

或者还有其他方法可以检索这些行? 谢谢!

Or Is there some other way to retrieve these rows? Thank you!

推荐答案

能否请您尝试以下操作,简单的grep就可以完成此任务.

Could you please try following, simple grep could handle this task.

grep -A2 -B2 '^s62234:' Input_file

更准确地说,您可以尝试使用以下命令将确切的字符串与grep匹配:

Also more accurately you could try following to match exact string with grep:

grep -C2  '^s62234:' Input_file

这篇关于如果AWK中满足条件,则打印行范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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