捕获使用awk线的范围内,多行记录不工作 [英] Capturing range of lines with awk, multiline records not working

查看:120
本文介绍了捕获使用awk线的范围内,多行记录不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解为什么在调用的时候RS我不能用AWK =和ORS =选项。有没有更好的方式来做到这一点?

本作品:

 的awk'/密码= [[:空间:]] * $ /'./test
    awk的'/密码= * $ /'./test

但我需要这个工作:

 的awk -v RS = -v ORS ='\\ n \\ n''/密码= * $ /'./test

示例文件(./test):

  User_Mike1:
           密码= 12345678
           somedata =什么    User_Mike2:
           密码= 12345678
           somedata =什么    User_Mike3:
           密码=
           somedata =什么    User_Mike4:
           密码= 12345678
           somedata =什么

所需的输出(含备案密码=',即一个空密码输入):

  User_Mike3:
           密码=
           somedata =什么

我还需要密码= *或密码= *以及线。抓我的头在这一个!

编辑:答案

我想我的问题是与文件本身,我手动添加新行和脚本工作正常。

在我来说,我想与密码记录= * =密码*密码=或登录=假:

 的awk -v RS = -v ORS ='\\ n''/密码= [*] / || /密码= [*] / || /登录= FALSE / || /密码= \\ n /'


解决方案

当您使用 RS = 换行符是不是你的记录结束了。所以,你的 $ 的模式不能满足你的期望。

尝试的awk -v RS = -v ORS ='\\ n \\ n''/密码= * \\ N /'./test

或使用 $ 尽量保持:的awk -v FS ='\\ n'-v RS = -v ORS ='\\ n \\ N''$ 2〜/ = * $ /'./test

I am trying to understand why I can't use awk when invoking the RS= and ORS= options. Is there a better way to do this?

This works:

    awk '/password =[[:space:]]*$/' ./test
    awk '/password =  *$/' ./test

But I need this to work:

    awk -v RS= -v ORS='\n\n' '/password = *$/' ./test

Sample file (./test) :

    User_Mike1:
           password = 12345678
           somedata = whatever

    User_Mike2:
           password = 12345678
           somedata = whatever

    User_Mike3:
           password = 
           somedata = whatever

    User_Mike4:
           password = 12345678
           somedata = whatever

Desired output (the record containing 'password =' i.e. an empty password entry):

    User_Mike3:
           password = 
           somedata = whatever

I also need the lines with password =* or password = * as well. Scratching my head over this one!

EDIT: ANSWER

I think my problem was with the file itself, I added new lines manually and the script worked correctly..

In my case I want the records with password =* password = * password = or login = false:

    awk -v RS= -v ORS='\n' '/password = [*]/ || /password =[*]/ || /login = false/ || /password = \n/'

解决方案

When you use RS= newline isn't the end of your record anymore. So your $ in the pattern is failing to match what you expect.

Try awk -v RS= -v ORS='\n\n' '/password = *\n/' ./test

Or to keep using $ try: awk -v FS='\n' -v RS= -v ORS='\n\n' '$2 ~ /= *$/' ./test

这篇关于捕获使用awk线的范围内,多行记录不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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