(Modified Descriptive) Shell scripting to 如何显示搜索字符串的具体内容? [英] (Modified Descriptive) Shell scripting to How to display the specific contents of the search string?
问题描述
这是我的查询:
/path/newdir/newtext.csv
newtext.csv 如下所示:
newtext.csv looks like below :
第 1 行2号线样品编号:123456789(第3行)|||||时间:2012-05-29T10:21:06Z(第21行)|||超时:2012-05-29T13:07:46Z(第 30 行)
line 1 line 2 Sample Number: 123456789 (line no. 3) | | | | | Time In: 2012-05-29T10:21:06Z (line no. 21) | | | Time Out: 2012-05-29T13:07:46Z (line no. 30)
第 1 行2号线样品编号:363214563(第3行)|||||时间:2012-05-29T10:21:06Z(第21行)|||超时:2012-05-29T13:07:46Z(第 30 行)
line 1 line 2 Sample Number: 363214563 (line no. 3) | | | | | Time In: 2012-05-29T10:21:06Z (line no. 21) | | | Time Out: 2012-05-29T13:07:46Z (line no. 30)
第 1 行2号线样品编号:987654321(第3行)|||||时间:2012-05-29T10:21:06Z(第21行)|||超时:2012-05-29T13:07:46Z(第 30 行)
line 1 line 2 Sample Number: 987654321 (line no. 3) | | | | | Time In: 2012-05-29T10:21:06Z (line no. 21) | | | Time Out: 2012-05-29T13:07:46Z (line no. 30)
假设一个 newtext.csv 中有这样 100 条记录所以,现在我需要输入的 i/p 字符串的参数,这是下面的内容
Assume there are such 100 records in a newtext.csv So, now i need the parameters of the entered i/p string, which is something below
示例输入搜索字符串:
123456789
示例输出:
Sample Number: 123456789
Time In: 2012-05-29T10:21:06Z
Time Out: 2012-05-29T13:07:46Z
这正是我需要的.你能帮我吗?
This is what exactly i need. Can you please help me ?
推荐答案
Perl
应该更适合完成这个任务.
Perl
should be more suitable for this task.
$ cat newtext.csv
line 1 line 2 Sample Number: 123456789 (line no. 3) | | | | | Time In: 2012-05-29T10:21:06Z (line no. 21) | | | Time Out: 2012-05-29T13:07:46Z (line no. 30)
line 1 line 2 Sample Number: 363214563 (line no. 3) | | | | | Time In: 2012-05-29T10:21:06Z (line no. 21) | | | Time Out: 2012-05-29T13:07:46Z (line no. 30)
line 1 line 2 Sample Number: 987654321 (line no. 3) | | | | | Time In: 2012-05-29T10:21:06Z (line no. 21) | | | Time Out: 2012-05-29T13:07:46Z (line no. 30)
使用单行就可以得到预期的结果,无论字段数如何,只需编写正则表达式即可.
You can get the expected result using a one-liner, no matter the number of fields, just compose the regular expression.
# perl -lane '/(Sample Number:\s*\d+).*(Time In:\s*\S+).*(Time Out:\s*\S+)/;print $1."\n".$2."\n".$3' newtext.csv
Sample Number: 123456789
Time In: 2012-05-29T10:21:06Z
Time Out: 2012-05-29T13:07:46Z
Sample Number: 363214563
Time In: 2012-05-29T10:21:06Z
Time Out: 2012-05-29T13:07:46Z
Sample Number: 987654321
Time In: 2012-05-29T10:21:06Z
Time Out: 2012-05-29T13:07:46Z
这篇关于(Modified Descriptive) Shell scripting to 如何显示搜索字符串的具体内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!