如何从以下输入文件中过滤所需的行?

时间:2018-04-18 05:30:00

标签: bash shell unix awk grep

输入数据

%                Job1      EXEC +10 03:28 (03:23)  #J18911
                 Job2      EXEC +10 12:56 (01:55)  #J1766
                 Job3      EXEC +10 04/05          #J333460
                 Job4      EXEC +10 02/26 (01:10)  #J3322
>>every run      Job5      EXEC +10 04:58 (02:23)  #J189115; <04/18
>>every run      Job6      EXEC +10 16:07 (00:23)  #J189115; &0:05
                 Job7      EXEC +10 14:00 (01:02)  #J260721; <04/18

案例1(输出):只想打印前一天运行作业的那些行。 (只有“EXEC”中的第二列包含“/”)

                 Job3      EXEC +10 04/05          #J333460
                 Job4      EXEC +10 02/26 (01:10)  #J3322

案例2(输出):想要打印当天正在运行作业的所有行。 (“EXEC”的第二栏不包含日期,即04/05或02/26)

%                Job1      EXEC +10 03:28 (03:23)  #J18911
                 Job2      EXEC +10 12:56 (01:55)  #J1766
                 Job3      EXEC +10 04/05          #J333460
                 Job4      EXEC +10 02/26 (01:10)  #J3322
>>every run      Job5      EXEC +10 04:58 (02:23)  #J189115; <04/18
>>every run      Job6      EXEC +10 16:07 (00:23)  #J189115; &0:05
                 Job7      EXEC +10 14:00 (01:02)  #J260721; <04/18

1 个答案:

答案 0 :(得分:1)

输入文件:

# Print lines with slash (/) in 4th field:
>grep 'EXEC.*/.* ' inputfile
Job3      EXEC +10 04/05          #J333460
Job4      EXEC +10 02/26 (01:10)  #J3322

# Print lines with colon (:) in 4th field
>grep 'EXEC[^(]*:.* ' inputfile
Job1      EXEC +10 03:28 (03:23)  #J18911
Job2      EXEC +10 12:56 (01:55)  #J1766
Job5      EXEC +10 04:58 (02:23)  #J189115; <04/18
Job6      EXEC +10 16:07 (00:23)  #J189115; &0:05
Job7      EXEC +10 14:00 (01:02)  #J260721; <04/18

您可以使用reduce生成以下输出:

let request = {name: 'John', lastname : 'Doe', address: 'bla bla', age : 67, gender : 'M' }
let fillable = ['name', 'lastname', 'address']

let newObj = fillable.reduce((c, v) => Object.assign(c, {[v]: request[v] || ""}), {});

console.log(newObj);