我试图理解为什么在调用RS =和ORS =选项时我不能使用awk。有更好的方法吗?
这有效:
awk '/password =[[:space:]]*$/' ./test
awk '/password = *$/' ./test
但我需要这个工作:
awk -v RS= -v ORS='\n\n' '/password = *$/' ./test
示例文件(./test):
User_Mike1:
password = 12345678
somedata = whatever
User_Mike2:
password = 12345678
somedata = whatever
User_Mike3:
password =
somedata = whatever
User_Mike4:
password = 12345678
somedata = whatever
所需的输出(包含'password ='的记录,即空密码条目):
User_Mike3:
password =
somedata = whatever
我也需要使用password = *或password = *的行。抓住我的头!
编辑:答案
我认为我的问题在于文件本身,我手动添加新行并且脚本正常工作..
在我的情况下,我想要使用password = * password = * password =或login = false:
的记录 awk -v RS= -v ORS='\n' '/password = [*]/ || /password =[*]/ || /login = false/ || /password = \n/'
答案 0 :(得分:4)
当您使用RS=
时,换行符不再是您记录的结尾。因此,模式中的$
无法与您期望的相匹配。
尝试awk -v RS= -v ORS='\n\n' '/password = *\n/' ./test
或继续使用$
尝试:awk -v FS='\n' -v RS= -v ORS='\n\n' '$2 ~ /= *$/' ./test
答案 1 :(得分:0)
下一个perl
perl -00 -lanE 'say "$_\n" if m/password\s*=\s*\*?\s*\n/' <<EOF
User_Mike1:
password = 12345678
somedata = whatever
User_Mike2:
password = *
somedata = whatever
User_Mike3:
password =
somedata = whatever
User_Mike4:
password = 12345678
somedata = whatever
EOF
打印
User_Mike2:
password = *
somedata = whatever
User_Mike3:
password =
somedata = whatever