可以 grep 文件查找模式,然后根据模式的部分对找到的行进行排序吗?

发布于 2024-11-19 19:33:32 字数 572 浏览 3 评论 0原文

我将许多单独序列化的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风疏影 2024-11-26 19:33:33

我不确定 grep 是从哪里来的,因为你的所有行似乎都与模式匹配。但无论如何,您可以单独使用 sort 对示例输入进行排序:

sort -t\" -k4 data.txt

它忽略文本的“真实”结构,只是将 " 视为分隔符,因此它又快又脏,但它可以按照您想要的方式排序:

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:

sort -t\" -k4 data.txt

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 the sort command.

赤濁 2024-11-26 19:33:33

以下是如何根据名称对行进行排序的方法。我已经分解了这些步骤,以便您可以看到中间输出。

> cat data.txt
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";}

现在,我们将使用“sed”命令通过正则表达式提取名称。然后,我们输出名称、制表符,然后输出原始行,以便我们对其进行排序:

> cat data.txt | sed -rn 's/[^"]+"name";s:[0-9]+:"([^"]+)".*/\1\t\0/p'
John Doe        a:2:{s:4:"name";s:8:"John Doe";s:3:"age";s:2:"20";}
Jane Doe        a:2:{s:4:"name";s:8:"Jane Doe";s:3:"age";s:2:"15";}
Steven Tyler    a:2:{s:4:"name";s:12:"Steven Tyler";s:3:"age";s:2:"35";}
Jim Morrison    a:2:{s:4:"name";s:12:"Jim Morrison";s:3:"age";s:2:"25";}
Apple Paltrow   a:2:{s:4:"name";s:13:"Apple Paltrow";s:3:"age";s:2:"75";}
Drew Nickels    a:2:{s:4:"name";s:12:"Drew Nickels";s:3:"age";s:2:"34";}
Jason Proop     a:2:{s:4:"name";s:11:"Jason Proop";s:3:"age";s:2:"36";}

此 sed 命令要求“名称”值是该行中第一个带引号的字符串。如果您不能保证您应该使用 php 脚本来实现此步骤,并使用本机 php 函数反序列化数据。如果“name”不存在或者不是该行中第一个带引号的字符串,则该行将被跳过。有关 sed 的更多信息,在线有很多资源。

现在名称位于行的前面,我们可以使用普通的 unix sort 命令对它们进行排序:

> cat data.txt | sed -rn 's/[^"]+"name";s:[0-9]+:"([^"]+)".*/\1\t\0/p' | sort
Apple Paltrow   a:2:{s:4:"name";s:13:"Apple Paltrow";s:3:"age";s:2:"75";}
Drew Nickels    a:2:{s:4:"name";s:12:"Drew Nickels";s:3:"age";s:2:"34";}
Jane Doe        a:2:{s:4:"name";s:8:"Jane Doe";s:3:"age";s:2:"15";}
Jason Proop     a:2:{s:4:"name";s:11:"Jason Proop";s:3:"age";s:2:"36";}
Jim Morrison    a:2:{s:4:"name";s:12:"Jim Morrison";s:3:"age";s:2:"25";}
John Doe        a:2:{s:4:"name";s:8:"John Doe";s:3:"age";s:2:"20";}
Steven Tyler    a:2:{s:4:"name";s:12:"Steven Tyler";s:3:"age";s:2:"35";}

现在我们已经对行进行了排序,我们只需要去掉行前面的普通名称即可:

> cat data.txt | sed -rn 's/[^"]+"name";s:[0-9]+:"([^"]+)".*/\1\t\0/p' | sort | cut -f2
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:8:"Jane Doe";s:3:"age";s:2:"15";}
a:2:{s:4:"name";s:11:"Jason Proop";s:3:"age";s:2:"36";}
a:2:{s:4:"name";s:12:"Jim Morrison";s:3:"age";s:2:"25";}
a:2:{s:4:"name";s:8:"John Doe";s:3:"age";s:2:"20";}
a:2:{s:4:"name";s:12:"Steven Tyler";s:3:"age";s:2:"35";}

享受吧!

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.

> cat data.txt
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";}

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:

> cat data.txt | sed -rn 's/[^"]+"name";s:[0-9]+:"([^"]+)".*/\1\t\0/p'
John Doe        a:2:{s:4:"name";s:8:"John Doe";s:3:"age";s:2:"20";}
Jane Doe        a:2:{s:4:"name";s:8:"Jane Doe";s:3:"age";s:2:"15";}
Steven Tyler    a:2:{s:4:"name";s:12:"Steven Tyler";s:3:"age";s:2:"35";}
Jim Morrison    a:2:{s:4:"name";s:12:"Jim Morrison";s:3:"age";s:2:"25";}
Apple Paltrow   a:2:{s:4:"name";s:13:"Apple Paltrow";s:3:"age";s:2:"75";}
Drew Nickels    a:2:{s:4:"name";s:12:"Drew Nickels";s:3:"age";s:2:"34";}
Jason Proop     a:2:{s:4:"name";s:11:"Jason Proop";s:3:"age";s:2:"36";}

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:

> cat data.txt | sed -rn 's/[^"]+"name";s:[0-9]+:"([^"]+)".*/\1\t\0/p' | sort
Apple Paltrow   a:2:{s:4:"name";s:13:"Apple Paltrow";s:3:"age";s:2:"75";}
Drew Nickels    a:2:{s:4:"name";s:12:"Drew Nickels";s:3:"age";s:2:"34";}
Jane Doe        a:2:{s:4:"name";s:8:"Jane Doe";s:3:"age";s:2:"15";}
Jason Proop     a:2:{s:4:"name";s:11:"Jason Proop";s:3:"age";s:2:"36";}
Jim Morrison    a:2:{s:4:"name";s:12:"Jim Morrison";s:3:"age";s:2:"25";}
John Doe        a:2:{s:4:"name";s:8:"John Doe";s:3:"age";s:2:"20";}
Steven Tyler    a:2:{s:4:"name";s:12:"Steven Tyler";s:3:"age";s:2:"35";}

Now we've got our lines sorted, we just need to get rid of the plain names at the front of the lines:

> cat data.txt | sed -rn 's/[^"]+"name";s:[0-9]+:"([^"]+)".*/\1\t\0/p' | sort | cut -f2
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:8:"Jane Doe";s:3:"age";s:2:"15";}
a:2:{s:4:"name";s:11:"Jason Proop";s:3:"age";s:2:"36";}
a:2:{s:4:"name";s:12:"Jim Morrison";s:3:"age";s:2:"25";}
a:2:{s:4:"name";s:8:"John Doe";s:3:"age";s:2:"20";}
a:2:{s:4:"name";s:12:"Steven Tyler";s:3:"age";s:2:"35";}

Enjoy!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文