可以 grep 文件查找模式,然后根据模式的部分对找到的行进行排序吗?
我将许多单独序列化的 PHP 数组存储到一个文件中。文件的每一行都包含一个序列化数组。例如:
a:2:{s:4:"name";s:8:"John Doe";s:3:"age";s:2:"20";}
a:2:{s:4:"name";s:8:"Jane Doe";s:3:"age";s:2:"15";}
a:2:{s:4:"name";s:12:"Steven Tyler";s:3:"age";s:2:"35";}
a:2:{s:4:"name";s:12:"Jim Morrison";s:3:"age";s:2:"25";}
a:2:{s:4:"name";s:13:"Apple Paltrow";s:3:"age";s:2:"75";}
a:2:{s:4:"name";s:12:"Drew Nickels";s:3:"age";s:2:"34";}
a:2:{s:4:"name";s:11:"Jason Proop";s:3:"age";s:2:"36";}
这是我的问题:
是否可以按照以下模式 grep 该文件: "name"*"*"
然后,我想根据内容对找到的行进行排序第二个通配符的。
I am storing a number of individually serialized PHP arrays to a file. Each line of the file contains one serialized array. For example:
a:2:{s:4:"name";s:8:"John Doe";s:3:"age";s:2:"20";}
a:2:{s:4:"name";s:8:"Jane Doe";s:3:"age";s:2:"15";}
a:2:{s:4:"name";s:12:"Steven Tyler";s:3:"age";s:2:"35";}
a:2:{s:4:"name";s:12:"Jim Morrison";s:3:"age";s:2:"25";}
a:2:{s:4:"name";s:13:"Apple Paltrow";s:3:"age";s:2:"75";}
a:2:{s:4:"name";s:12:"Drew Nickels";s:3:"age";s:2:"34";}
a:2:{s:4:"name";s:11:"Jason Proop";s:3:"age";s:2:"36";}
Here is my question:
Is it possible to grep this file for the following pattern: "name"*"*"
Afterwards, I would like to sort the lines that are found based on the contents of the second wildcard.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定 grep 是从哪里来的,因为你的所有行似乎都与模式匹配。但无论如何,您可以单独使用
sort
对示例输入进行排序:它忽略文本的“真实”结构,只是将
"
视为分隔符,因此它又快又脏,但它可以按照您想要的方式排序:http://ideone.com/ZugIX
如果您确实需要要 grep 查找
"name".*".*"
,您可以先执行此操作,然后将输出通过管道传输到sort
命令。I'm not sure where grepping comes into this, as all your lines seem to match the pattern. But anyway, you can use
sort
on its own to sort your sample input:It's ignoring the "real" structure of the text, it's just treating
"
as a delimiter, so it's quick and dirty but it sorts how you want. Here it is in action:http://ideone.com/ZugIX
If you do need to grep for
"name".*".*"
, you can just do that first and pipe the output to thesort
command.以下是如何根据名称对行进行排序的方法。我已经分解了这些步骤,以便您可以看到中间输出。
现在,我们将使用“sed”命令通过正则表达式提取名称。然后,我们输出名称、制表符,然后输出原始行,以便我们对其进行排序:
此 sed 命令要求“名称”值是该行中第一个带引号的字符串。如果您不能保证您应该使用 php 脚本来实现此步骤,并使用本机 php 函数反序列化数据。如果“name”不存在或者不是该行中第一个带引号的字符串,则该行将被跳过。有关 sed 的更多信息,在线有很多资源。
现在名称位于行的前面,我们可以使用普通的 unix sort 命令对它们进行排序:
现在我们已经对行进行了排序,我们只需要去掉行前面的普通名称即可:
享受吧!
Here is how you can sort your lines based on the name. I've broken down the steps so you can see the intermediate output.
Now, we'll use the 'sed' command to extract the name using a regex. We then output the name, a tab, then the original line so we can sort it:
This sed command requires the 'name' value to be the first quoted string on the line. If you can't guarantee that you should probably implement this step with a php script and deserialize the data using the native php functions. If 'name' is not present or it's not the first quoted string in the line the line will be skipped. For more information on sed, there are many resources online.
Now that the names are first on the line, we can use the normal unix sort command to sort them:
Now we've got our lines sorted, we just need to get rid of the plain names at the front of the lines:
Enjoy!