【问题标题】:How can I make a log file from a specific application database-file in bash?如何从 bash 中的特定应用程序数据库文件创建日志文件?
【发布时间】:2014-05-23 08:33:07
【问题描述】:

我有一个这样的特定应用程序数据库文件:

•r vbc.vvc lin K$³ñ123456 Œ Œ P P P ;šÉÿ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ÿÿÿÿ Œ ÿÿÿÿ Œ P T X Œ P 999999999 ëu q0 ...ª¤ ;šÉÿOverLimitLog ÿÿ 06/27/2013 00:05:55 RS_A1_6 hijitso 183.49 OverLow ÿÿ 06/27/2013 00:16:30 qaser hijitso 993.06/87:30 46 yuikl hijitso 993.08 hffvcgcf YY 2013年6月27日0时25分01秒gcxsd hijitso 999.18 hffvcgcf YY 2013年6月27日〇时40分45秒hgdrt sdfcdsfb 0.00普通YY 2013年6月27日6时25分55秒khuhukjmnj,升sdfcdsfa 13.13 hasfldkdgh YY 2013年6月27日6点25分55秒khuhukjmnj,升13.13 sdfcdsfc YY hasfldkdgh 2013年6月27日6点25分55秒khuhukjmnj,升13.13 sdfcdsfb YY hasfldkdgh 2013年6月27日6时27分16秒khuhukjmnj,升sdfcdsfa 0.00 正常 ÿÿ 06/27/2013 06:27:16 khuhukjmnj,l sdfcdsfc 0.00 正常 fhfx 06/27/2013 06:27:16 khuhukjmnj,l sdfcdsfb 0.00 也mal ÿÿ 2013 年 6 月 27 日 06:28:05 khuhukjmnj,l sdfcdsfa 10.79 hasfldkdgh ÿÿ 2013 年 6 月 27 日 06:28:05 khuhukjmnj,l sdfcdsfc 10.79 hasfldkdgh ÿ 06/27:03 kdhukjmnj, 06: 9.38 hasfldkdgh ÿÿ 06/27/2013 06:30:05 khuhukjmnj,l sdfcdsfb 9.38 hasfldkdgh ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ

我想把它转换成这种格式:

06/27/2013  06:27:16 khuhukjmnj,l      sdfcdsfc              0.00 Normal

我必须说,我的文件不是常规的文本格式,在 gedit 中打开时有一些不可读的字符,但 emacs23 可以打开这些文件。

【问题讨论】:

  • 最好检查一下这个文件是如何生成的。
  • 不知道为什么日志文件出现6行。
  • 它是一个应用程序数据库。该软件可以读取它并在其内部显示它

标签: bash unix shellcode logfile


【解决方案1】:

使用 GNU awk,您可以执行以下操作:

$ awk 'NR>1{print RS, $1, $2, $3, $4, $5, $6}' RS='fhfx' OFS='\t' file
fhfx    06/27/2013  00:05:55    RS_A1_6 hijitso 183.49  OverLow
fhfx    06/27/2013  00:16:30    qaser   hijitso 993.08  hffvcgcf
fhfx    06/27/2013  00:18:46    yuikl   hijitso 993.08  hffvcgcf
fhfx    06/27/2013  00:25:01    gcxsd   hijitso 999.18  hffvcgcf
fhfx    06/27/2013  00:40:45    hgdrt   sdfcdsfb    0.00    Normal
fhfx    06/27/2013  06:25:55    khuhukjmnj,l    sdfcdsfa    13.13   hasfldkdgh
fhfx    06/27/2013  06:25:55    khuhukjmnj,l    sdfcdsfc    13.13   hasfldkdgh
fhfx    06/27/2013  06:25:55    khuhukjmnj,l    sdfcdsfb    13.13   hasfldkdgh
fhfx    06/27/2013  06:27:16    khuhukjmnj,l    sdfcdsfa    0.00    Normal
fhfx    06/27/2013  06:27:16    khuhukjmnj,l    sdfcdsfc    0.00    Normal
fhfx    06/27/2013  06:27:16    khuhukjmnj,l    sdfcdsfb    0.00    Normal
fhfx    06/27/2013  06:28:05    khuhukjmnj,l    sdfcdsfa    10.79   hasfldkdgh
fhfx    06/27/2013  06:28:05    khuhukjmnj,l    sdfcdsfc    10.79   hasfldkdgh
fhfx    06/27/2013  06:30:05    khuhukjmnj,l    sdfcdsfc    9.38    hasfldkdgh
fhfx    06/27/2013  06:30:05    khuhukjmnj,l    sdfcdsfb    9.38    hasfldkdgh

或者如果你想让它更漂亮,你可以使用column

$ awk 'NR>1{print RS, $1, $2, $3, $4, $5, $6}' RS='fhfx' file | column -t 
fhfx  06/27/2013  00:05:55  RS_A1_6       hijitso   183.49  OverLow
fhfx  06/27/2013  00:16:30  qaser         hijitso   993.08  hffvcgcf
fhfx  06/27/2013  00:18:46  yuikl         hijitso   993.08  hffvcgcf
fhfx  06/27/2013  00:25:01  gcxsd         hijitso   999.18  hffvcgcf
fhfx  06/27/2013  00:40:45  hgdrt         sdfcdsfb  0.00    Normal
fhfx  06/27/2013  06:25:55  khuhukjmnj,l  sdfcdsfa  13.13   hasfldkdgh
fhfx  06/27/2013  06:25:55  khuhukjmnj,l  sdfcdsfc  13.13   hasfldkdgh
fhfx  06/27/2013  06:25:55  khuhukjmnj,l  sdfcdsfb  13.13   hasfldkdgh
fhfx  06/27/2013  06:27:16  khuhukjmnj,l  sdfcdsfa  0.00    Normal
fhfx  06/27/2013  06:27:16  khuhukjmnj,l  sdfcdsfc  0.00    Normal
fhfx  06/27/2013  06:27:16  khuhukjmnj,l  sdfcdsfb  0.00    Normal
fhfx  06/27/2013  06:28:05  khuhukjmnj,l  sdfcdsfa  10.79   hasfldkdgh
fhfx  06/27/2013  06:28:05  khuhukjmnj,l  sdfcdsfc  10.79   hasfldkdgh
fhfx  06/27/2013  06:30:05  khuhukjmnj,l  sdfcdsfc  9.38    hasfldkdgh
fhfx  06/27/2013  06:30:05  khuhukjmnj,l  sdfcdsfb  9.38    hasfldkdgh

【讨论】:

  • 非常好的答案!我喜欢 bash。
  • 现在我如何才能从数据开始?
  • 我的真实文件有“ÿÿ”字符。在“ÿÿ”之前,我有管道字符,但这里没有显示。第二个问题:如何从我的文件中删除它?
  • @user3121138 我不确定我理解你的意思。也许您应该编辑您的问题以包含更真实的输入和输出示例。
  • @user3121138 尝试将print RS, $1, ... 更改为print RT, $1, ...RT 变量保存与RS 变量匹配的分隔符——即当前记录之后 的分隔符。您还可以使用持有变量来保留上一条记录的RT
【解决方案2】:

你可以试试这个:

cat output.txt | sed 's/fhfx/\n&/g' | sed 's/ÿÿ *//g' | sed  '/fhfx/!d'

结果是:

fhfx 06/27/2013 00:05:55 RS_A1_6 hijitso 183.49 OverLow fhfx 06/27/2013 00:16:30 qaser hijitso 993.08 hffvcgcf fhfx 06/27/2013 00:18:46 yuikl hijitso 993.08 hffvcgcf fhfx 06/27/2013 00:25:01 gcxsd hijitso 999.18 hffvcgcf fhfx 06/27/2013 00:40:45 hgdrt sdfcdsfb 0.00 正常 fhfx 06/27/2013 06:25:55 khuhukjmnj,l sdfcdsfa 13.13 hasfldkdgh fhfx 06/27/2013 06:25:55 khuhukjmnj,l sdfcdsfc 13.13 hasfldkdgh fhfx 06/27/2013 06:25:55 khuhukjmnj,l sdfcdsfb 13.13 hasfldkdgh fhfx 06/27/2013 06:27:16 khuhukjmnj,l sdfcdsfa 0.00 正常 fhfx 06/27/2013 06:27:16 khuhukjmnj,l sdfcdsfc 0.00 正常 fhfx 06/27/2013 06:27:16 khuhukjmnj,l sdfcdsfb 0.00 正常 fhfx 06/27/2013 06:28:05 khuhukjmnj,l sdfcdsfa 10.79 hasfldkdgh fhfx 06/27/2013 06:28:05 khuhukjmnj,l sdfcdsfc 10.79 hasfldkdgh fhfx 06/27/2013 06:30:05 khuhukjmnj,l sdfcdsfc 9.38 hasfldkdgh fhfx 06/27/2013 06:30:05 khuhukjmnj,l sdfcdsfb 9.38 hasfldkdgh

你在前最后一行有问题,但我不知道它是否可以。 我不知道您是否要对齐列等...

【讨论】:

  • 您不需要 cat,因为 sed 会在命令行上接受文件名 - 请参阅 UUOC
  • @Iain 是的,但那只是一个例子,因为我不知道日志文件的来源是什么。也许他想tail 它或者它是其他命令的结果。
猜你喜欢
  • 2015-05-14
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
  • 2015-04-10
  • 2013-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多