如何编写shell脚本来显示以下输出?

时间:2012-06-04 09:19:53

标签: shell

newtext.csv如下所示:

Record 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) 

Record 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) 

Record 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字符串的参数,这是下面的内容

Input
Enter the search String :
123456789

Output

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

这正是我需要的。你可以帮我解决上面提到的格式的shell脚本吗?

1 个答案:

答案 0 :(得分:1)

好的,输入和所需的输出有点奇怪,但要获得你想要的东西仍然不难,请尝试以下方法:

var=123456789
awk -v "var=$var" --exec /dev/stdin newtext.csv <<'EOF'
  ($7 == var) {
    printf("Sample Number is, Sample Number: %s\n", $7);
    printf("Connected Time is, Time In: %s\n", $18);
    printf("Disconnected Time is, Time Out: %s\n", $27);
  }
EOF