简体   繁体   English

在sed / awk start表达式上方打印四行

[英]print the four lines above the sed/awk start expression

I have the following sed command that fetches back a whole chunk of a log that has an <interface> XML within it that's being printed into a txt file: 我有以下sed命令,该命令可取回一整条日志,该日志中有一个<interface> XML,并将其打印到txt文件中:

sed -n '/(StartingExpression)/{:start /<\/Interface>/!{N;b start};/(SomeValueInTheXML)"/p}' *.log > File.txt

The issue is that I have some dynamic values (time stamp) that's always 4 lines above the (StartingExpression) ... is there a way to print something like: 问题是我有一些动态值(时间戳)总是在(StartingExpression)上方4行...可以打印以下内容:

line of (StartingExpression) -4

?

I found this question: search (eg awk, grep, sed) for string, then look for X lines above and another string below but the solution isn't really clear :\\ 我发现了这个问题: 搜索(例如awk,grep,sed)字符串,然后在上面的X行和下面的另一个字符串中查找,但是解决方案还不是很清楚:

Thanks for the help. 谢谢您的帮助。

also, if anyone has a good source to learn sed, I'll be thankful if you post it :) 另外,如果任何人都有学习sed的好资源,如果您发布它,我将不胜感激:)

EDIT per request: 按请求进行编辑:

one
two
1/1/2015
line 1
12345
(StartingExpression)
<Interface>
   <A>
   Name=Andy
   </A>
</Interface>
three
four
1/1/2015
line 1
12345
(StartingExpression)
<Interface>
   <A>
   Name=John
   </A>
</Interface>
hello
world

I would like to print from 1/1/2015 (which is 3 lines above (StartingExpression) - this can be dynamic as its a date ) until </Interface> 我想从1/1/2015 1月1日(StartingExpression)打印(它在(StartingExpression)上方3行-作为日期可以是动态的),直到</Interface>

EDIT: I forgot to mention, there can be multiple instances of these interfaces... How do you also ensure to print ONLY the one that has Name=Andy ? 编辑:我忘了提及,这些接口可以有多个实例...您还如何确保仅打印具有Name=Andy实例?

file.txt: file.txt:

1/1/2015
line 1
12345
(StartingExpression)
<Interface>
   <A>
   Name=Andy
   </A>
</Interface>

This awk idiom will print n lines preceding and including pattern. 这个awk惯用语将在模式之前(包括模式)打印n行。

$awk -v n="$plines" '{a[p]=$0; p=(p+1)%n} /pattern/{for(i=p;i<p+n;i++) print a[i%n]}' file

For example 例如

$awk -v n=5 '{a[p]=$0; p=(p+1)%n} /20/{for(i=p;i<p+n;i++) print a[i%n]}' <(seq 10 99)

16
17
18
19
20

UPDATE for additional logic. 更新以获取其他逻辑。

You can incorporate printing after pattern match easily as well. 您也可以轻松地在图案匹配后合并打印。 This script will print the lines before; 该脚本将打印之前的行; and now after the match until the end of close tag. 现在比赛结束后,直到结束标记结束。

$ awk -v n=4 '           {a[p]=$0; p=(p+1)%n} 
   /(StartingExpression)/{for(i=p;i<p+n-1;i++) print a[i%n];f=1} 
                        f{print} 
            /\/Interface/{exit}' file

1/1/2015
line 1
12345
(StartingExpression)
<Interface>
   <A>
   </A>
</Interface>

UPDATE: filter based on attribute value 更新:根据属性值过滤

It's easier at this point just pass through another script instead of rewriting it. 此时,通过另一个脚本而不是重写它会更容易。 Replace the exit with f=0, the first script will output all the matching records and filter in a second script the record of interest. 将出口替换为f = 0,第一个脚本将输出所有匹配的记录,并在第二个脚本中过滤感兴趣的记录。

$ awk -v n=4 '           {a[p]=$0; p=(p+1)%n}
   /(StartingExpression)/{for(i=p;i<p+n-1;i++) print a[i%n];f=1}
                        f{print}
            /\/Interface/{f=0}' file
| awk 'BEGIN{ORS=RS="</Interface>\n"} 
 /Name=Andy/'


1/1/2015
line 1
12345
(StartingExpression)
<Interface>
   <A>
   Name=Andy
   </A>
</Interface>

When you have a grep version that supports -A and -B, and your file is made with fixed lines, you can use 如果您拥有支持-A和-B的grep版本,并且文件是用固定行制作的,则可以使用

grep -A2 -B6 "Name=Andy" request

Using your vi-knowledge can help for making the following command: 使用您的vi知识可以帮助执行以下命令:

echo "/(StartingExpression)/-3,/<.Interface>/ p" | ed -s request

I used a . 我用了. for the slash in , I did not want to escape the character. 对于斜杠,我不想逃脱角色。 This will only work when Andy is the first name. 仅当Andy是名字时,这才起作用。

Making it working with Andy somewhere else becomes more tricky: 使它与Andy在其他地方一起工作变得更加棘手:

rm /tmp/andy
echo "/Name=Andy/
?(StartingExpression)
-3,/<.Interface>/ w /tmp/andy" | ed -s request 2>&1 >/dev/null
cat /tmp/andy
rm /tmp/andy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM