在for循环中使用awk和grep命令

时间:2022-07-11 20:12:25

Consider below code snippet:

请考虑以下代码段:

#! /bin/ksh
for i in `grep "ProcessOrderWebService-N" Orders.Log | grep "::stringFromNetwork  =  600001" | awk -F',' '{print $1}'`; do
    echo $i;
done;

2015-05-26
10:20:10
2015-05-26
10:20:49
2015-05-26
10:21:45

From the command prompt, when I run the command:

从命令提示符,当我运行命令时:

grep "ProcessOrderWebService-N" Orders.Log | grep "::stringFromNetwork  =  600001" | awk -F',' '{print $1}'

The output received is as below.

收到的输出如下。

2015-05-26 10:20:10
2015-05-26 10:20:49
2015-05-26 10:21:45

The grep pattern that I am searching in the file Orders.Log is as below:

我在Orders.Log文件中搜索的grep模式如下:

2015-05-26 10:20:10,847 : ProcessOrderWebService-N|220082|1|::stringFromNetwork  =  600001 - reference number is 26000033

Basically I want to get the output in for loop as 2015-05-26 10:20:10. Where as in the above case, the date and time are printed in different lines.

基本上我想在for循环中获得输出为2015-05-26 10:20:10。在上述情况下,日期和时间以不同的行打印。

2 个解决方案

#1


0  

Multiple greps and awks in pipes are never needed. Mostly the job can be done with one single tool:

不需要管道中的多个greps和awks。大多数工作可以使用一个工具完成:

grep -oP '^[^,]+(?=.*ProcessOrderWebService-N.*::stringFromNetwork  =  600001)' file

Gives the desired list. If your really need a for loop, your have to change the internal field separator first:

提供所需的列表。如果你真的需要一个for循环,你必须先改变内部字段分隔符:

IFS=$'\n'
for line in $(grep -oP '^[^,]+(?=.*ProcessOrderWebService-N.*::stringFromNetwork  =  600001)' file); do
  echo $line
done

But, I recommend a while loop, when you deal with lines:

但是,当你处理线条时,我建议使用while循环:

grep -oP '^[^,]+(?=.*ProcessOrderWebService-N.*::stringFromNetwork  =  600001)' file | while read line; do
  echo $line
done

#2


2  

The problem is that you're using a for loop, which iterates over ($IFS separated) words not lines. Use a while read loop to iterate over lines:

问题是你正在使用for循环,迭代($ IFS分隔)单词而不是行。使用while循环迭代行:

#!/bin/ksh
grep "ProcessOrderWebService-N" Orders.Log |
grep "::stringFromNetwork  =  600001" |
awk -F',' '{print $1}' |
while IFS= read -r line; do
    echo "$line"
done

However, as chaos suggests, all the work can be done in a single tool. On your solaris box, awk would be the better tool:

但是,正如混乱所暗示的那样,所有工作都可以在一个工具中完成。在你的solaris盒子上,awk将是更好的工具:

awk -F, '/ProcessOrderWebService-N/ && /::stringFromNetwork  =  600001/ {print $1}' Orders.Log

#1


0  

Multiple greps and awks in pipes are never needed. Mostly the job can be done with one single tool:

不需要管道中的多个greps和awks。大多数工作可以使用一个工具完成:

grep -oP '^[^,]+(?=.*ProcessOrderWebService-N.*::stringFromNetwork  =  600001)' file

Gives the desired list. If your really need a for loop, your have to change the internal field separator first:

提供所需的列表。如果你真的需要一个for循环,你必须先改变内部字段分隔符:

IFS=$'\n'
for line in $(grep -oP '^[^,]+(?=.*ProcessOrderWebService-N.*::stringFromNetwork  =  600001)' file); do
  echo $line
done

But, I recommend a while loop, when you deal with lines:

但是,当你处理线条时,我建议使用while循环:

grep -oP '^[^,]+(?=.*ProcessOrderWebService-N.*::stringFromNetwork  =  600001)' file | while read line; do
  echo $line
done

#2


2  

The problem is that you're using a for loop, which iterates over ($IFS separated) words not lines. Use a while read loop to iterate over lines:

问题是你正在使用for循环,迭代($ IFS分隔)单词而不是行。使用while循环迭代行:

#!/bin/ksh
grep "ProcessOrderWebService-N" Orders.Log |
grep "::stringFromNetwork  =  600001" |
awk -F',' '{print $1}' |
while IFS= read -r line; do
    echo "$line"
done

However, as chaos suggests, all the work can be done in a single tool. On your solaris box, awk would be the better tool:

但是,正如混乱所暗示的那样,所有工作都可以在一个工具中完成。在你的solaris盒子上,awk将是更好的工具:

awk -F, '/ProcessOrderWebService-N/ && /::stringFromNetwork  =  600001/ {print $1}' Orders.Log