1

这是我的查询:

/path/newdir/newtext.csv 

newtext.csv 如下所示:

记录 1

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)

记录 2

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)

记录 3

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 字符串的参数,如下所示

示例输入搜索字符串:

123456789

示例输出:

Sample Number: 123456789
Time In: 2012-05-29T10:21:06Z
Time Out: 2012-05-29T13:07:46Z

这正是我需要的。你能帮我么 ?

4

2 回答 2

1

对于普通输入*字符串,

grep -F "InputString" -A27 inputFile.csv | sed -n '1p;19p;$p'

对于模式(扩展正则表达式)*字符串,

grep -E "InputPattern" -A27 inputFile.csv | sed -n '1p;19p;$p'

脚本:

user$ cat script.sh

#!/bin/bash 
grep -F "$1" -A27 inputFile.csv | sed -n '1p;19p;$p'

user$ chmod +x script.sh
user$ ./script.sh "inputString"

编辑:

基于非行号的解决方案,

#!/bin/bash 
grep -F "$1" -A27 inputFile.csv |sed -n "/$1/p;/^Time\s[^:]*:/p"

*输入对于文件必须是唯一的。

于 2012-06-04T07:25:08.900 回答
0

试试这个

csv.txt(输入文件)

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)

csv.sh (代码)

echo "Enter your search string:"
read name
grep -A 10 "$name" csv.txt | grep -v "|" | awk -F "(" '{print $1}'

输出

Enter your search string: 123456789 

Sample Number: 123456789
Time In: 2012-05-29T10:21:06Z
Time Out: 2012-05-29T13:07:46Z
于 2012-06-04T07:21:24.563 回答