Команда Linux для обрезки файла журнала

Я бы использовал Python для чего-то подобного. Вот пример:

import csv

#Create a csv file with some data
myData = [["first_name", "second_name", "Grade"],
          ['Alex', 'Brian', 'A'],
          ['Tom', 'Smith', 'B']]

myFile1 = open('file1.csv', 'w')
with myFile1:
    writer = csv.writer(myFile1)
    writer.writerows(myData)

#Create a second csv file
myFile2 = open('file2.csv', 'w')

#Read the first file created with data
with open('file1.csv') as File:
    reader = csv.reader(File)
    for row in reader:
        #Print every row to the console
        print(row)
        if row[0] == "Alex":
           #If the first cell of the row says Alex, say hi and add the row to the second file
           print "Hi Alex"
           with myFile2:
             writer = csv.writer(myFile2)
             writer.writerow(row)
-1
11.03.2020, 14:18
2 ответа

Вы можете сделать это с помощью awkследующим образом:

awk -F '**' '{print $2}' input_file

ЧТОБЫ добавить время и дату, вы можете использовать что-то вроде:

awk -F '**' '{split($1,a," ");print a[1]" " a[2]" " $2}' input_file
0
28.04.2021, 23:20

Это вы можете попробовать:

grep -oP "took [[:digit:]]{3,} ms" file

Выход:

took 100 ms
took 110 ms
took 400 ms

Thanks. is it possible to list down the date/time as well? something like this as output :

2020-03-11 06:19:29.857 took 100 ms

2020-03-11 06:19:29.857 took 110 ms

Предполагая, что все записи имеют одинаковый формат, проще использовать cut, sedи grep:

.
cut -d' ' -f1,2,15-17 file | sed 's/*//g' | grep -P "took [[:digit:]]{3,} ms"

2020-03-11 06:19:29.857 took 100 ms
2020-03-11 06:19:29.857 took 110 ms
2020-03-11 06:19:29.857 took 400 ms
2
28.04.2021, 23:20

Теги

Похожие вопросы