如何在unix日志文件输出中拆分行

时间:2022-07-22 21:32:56

I'd like to be able to parse date and times out of a log file. Currently they're in the following format:

我希望能够从日志文件中解析日期和时间。目前,它们采用以下格式:

"02/Jun/2009:14:38:50" but i'd like to separate them in different columns using something available from a linux command line so that the resulting output looks as follows:

“02 / Jun / 2009:14:38:50”但我想使用linux命令行提供的东西将它们分成不同的列,以便生成的输出如下所示:

"02/Jun/2009" "14:38:50"

could someone please shed some light as to how this can be done?

请问有人可以说明如何做到这一点?

Regards

5 个解决方案

#1


Chris's answer is best if there are no lines with other formats. If you need to only affect lines with that specific format, here's something:

如果没有其他格式的行,Chris的答案是最好的。如果您只需要影响具有该特定格式的行,请执行以下操作:

cat log | sed -e 's/"\([^:]*\):\([^"]*\)"/"\1" "\2"/'

#2


If that's all that's on each line, maybe:

如果这就是每一行,那么可能:

cat file.txt | sed -e 's/:/" "/'

#3


Pure Bash:

while read line ; do echo ${line/:/\" \"}; done < logfile

#4


sub() function of awk returns 1, if substitution succeeds, so:

awk的sub()函数返回1,如果替换成功,则:

cat file.txt | awk 'sub(/:/,"\" \"")'

GNU awk can use gensub() function, so this is a sample of back reference below.

GNU awk可以使用gensub()函数,因此这是下面的后向引用示例。

cat file.txt | gawk '$0=gensub(/("[^:]*):([^"]*")/,"\\1\" \"\\2",1)'

#5


A biterscript to look for regular expression "&:&:&:&" and separate before and after the first colon. (& means any number of any characters).

一个biterscript寻找正则表达式“&:&:&:&”并在第一个冒号之前和之后分开。 (&表示任意数量的任何字符)。

var str log
cat "logfile" > $log
while ( { sen -r "^&:&:&:&^" $log } > 0 )
do
    var str datetime, date, time
    stex -r "^&:&:&:&^" $log > $datetime
    stex -p "]^:^" $datetime > $date    # String before the first : is date.
    stex -p "^:^[" $datetime > $time    # String after the first : is time.
    echo $date "\t" $time
done

To try, you can download biterscripting. Google it up, or installation instructions are at http://www.biterscripting.com .

要试试,您可以下载biterscripting。谷歌,或安装说明在http://www.biterscripting.com。

#1


Chris's answer is best if there are no lines with other formats. If you need to only affect lines with that specific format, here's something:

如果没有其他格式的行,Chris的答案是最好的。如果您只需要影响具有该特定格式的行,请执行以下操作:

cat log | sed -e 's/"\([^:]*\):\([^"]*\)"/"\1" "\2"/'

#2


If that's all that's on each line, maybe:

如果这就是每一行,那么可能:

cat file.txt | sed -e 's/:/" "/'

#3


Pure Bash:

while read line ; do echo ${line/:/\" \"}; done < logfile

#4


sub() function of awk returns 1, if substitution succeeds, so:

awk的sub()函数返回1,如果替换成功,则:

cat file.txt | awk 'sub(/:/,"\" \"")'

GNU awk can use gensub() function, so this is a sample of back reference below.

GNU awk可以使用gensub()函数,因此这是下面的后向引用示例。

cat file.txt | gawk '$0=gensub(/("[^:]*):([^"]*")/,"\\1\" \"\\2",1)'

#5


A biterscript to look for regular expression "&:&:&:&" and separate before and after the first colon. (& means any number of any characters).

一个biterscript寻找正则表达式“&:&:&:&”并在第一个冒号之前和之后分开。 (&表示任意数量的任何字符)。

var str log
cat "logfile" > $log
while ( { sen -r "^&:&:&:&^" $log } > 0 )
do
    var str datetime, date, time
    stex -r "^&:&:&:&^" $log > $datetime
    stex -p "]^:^" $datetime > $date    # String before the first : is date.
    stex -p "^:^[" $datetime > $time    # String after the first : is time.
    echo $date "\t" $time
done

To try, you can download biterscripting. Google it up, or installation instructions are at http://www.biterscripting.com .

要试试,您可以下载biterscripting。谷歌,或安装说明在http://www.biterscripting.com。