0

我知道星号以 csv 文件的形式创建自己的通话记录。出于我的目的,我需要如下所示格式化的通话记录。我用:

ls -l /var/spool/asterisk/monitor作为我的通话记录的基础,这会产生:

-rw------- 1 asterisk asterisk  112684 2013-02-07 17:24 20130207-172424-+15551235566-IN.wav
-rw------- 1 asterisk asterisk      44 2013-02-07 17:53 20130207-175311-+15554561122-IN.wav
-rw------- 1 asterisk asterisk 2019564 2013-02-07 18:00 20130207-175828-15554561122-OUT.wav
-rw------- 1 asterisk asterisk      44 2013-02-07 22:09 20130207-220805-15554561122-OUT.wav
-rw------- 1 asterisk asterisk      44 2013-02-07 22:12 20130207-221204-15551235566-OUT.wav
-rw------- 1 asterisk asterisk  111084 2013-02-07 22:13 20130207-221255-15551235566-OUT.wav
-rw------- 1 asterisk asterisk  364844 2013-02-07 22:39 20130207-223843-15558271212-OUT.wav
-rw------- 1 asterisk asterisk 4279404 2013-02-07 23:53 20130207-234836-5552785454-OUT.wav
-rw------- 1 asterisk asterisk      44 2013-02-08 00:00 20130208-000026-+15559813232-IN.wav

我需要帮助的部分是下面的命令。它可以工作并产生我想要的确切结果;但是,它对我来说似乎很笨重。可以缩短吗?

变量

YESTER=$(date -d "-24 hours" +"%Y-%m-%d-%H%M")
TODAY=$(date +"%Y-%m-%d-%H%M_UTC")

创建通话记录(我想更改的命令)

ls -l /var/spool/asterisk/monitor/ |grep '\.wav'|awk '{print $8 " " $5/1000000}'|sed -e 's/4\.4e\-05/NOT RECORDED/g' -e 's/\.wav//g' -e 's/-/ /g' -e 's/OUT/OUT - Approx Minutes:/g' -e 's/IN/IN - Approx Minutes:/g' -e 's/\(\.[0-9]\).*$/\1/g' -e 's/^.\{15\}/& UTC -/' -e 's/^.\{13\}/&:/' -e 's/^.\{11\}/&:/' -e 's/^.\{6\}/&-/' -e 's/^.\{4\}/& /' -e 's/+//g' > /var/spool/asterisk/monitor/call_logs/${YESTER}__${TODAY}-call-log.txt

为了便于阅读,这里是由行分隔的命令(不带 | ):

ls -l /var/spool/asterisk/monitor/
grep '\.wav'
awk '{print $8 " " $5/1000000}'
sed -e 's/4\.4e\-05/NOT RECORDED/g' 
-e 's/\.wav//g' 
-e 's/-/ /g' 
-e 's/OUT/OUT - Approx Minutes:/g'
-e 's/IN/IN - Approx Minutes:/g' 
-e 's/\(\.[0-9]\).*$/\1/g' 
-e 's/^.\{15\}/& UTC -/' 
-e 's/^.\{13\}/&:/' 
-e 's/^.\{11\}/&:/' 
-e 's/^.\{6\}/&-/' 
-e 's/^.\{4\}/& /' 
-e 's/+//g' 
> /var/spool/asterisk/monitor/call_logs/${YESTER}__${TODAY}-call-log.txt

输出:

2013 02-07 17:24:24 UTC - 15551235566 IN - Approx Minutes: 0.1
2013 02-07 17:53:11 UTC - 15554561122 IN - Approx Minutes: NOT RECORDED
2013 02-07 17:58:28 UTC - 15554561122 OUT - Approx Minutes: 2.0
2013 02-07 22:08:05 UTC - 15554561122 OUT - Approx Minutes: NOT RECORDED
2013 02-07 22:12:04 UTC - 15551235566 OUT - Approx Minutes: NOT RECORDED
2013 02-07 22:12:55 UTC - 15551235566 OUT - Approx Minutes: 0.1
2013 02-07 22:38:43 UTC - 15558271212 OUT - Approx Minutes: 0.3
2013 02-07 23:48:36 UTC - 5552785454 OUT - Approx Minutes: 4.2
2013 02-08 00:00:26 UTC - 15559813232 IN - Approx Minutes: NOT RECORDED
4

3 回答 3

0
$ cat tst.awk
{
   mins = $5 / 1000000
   mins = ( mins == "4.4e-05" ? "NOT RECORDED" : sprintf("%.1f",mins) )

   split($8,fname,/-\+?|\./)
   date = fname[1]
   time = fname[2]
   nrs  = fname[3]
   dir  = fname[4]

   printf "%s %s-%s ",substr(date,1,4),substr(date,5,2),substr(date,7,2)
   printf "%s:%s:%s UTC - ",substr(time,1,2),substr(time,3,2),substr(time,5,2)
   printf "%s %s - Approx Minutes: %s\n",nrs,dir,mins
}

$ awk -f tst.awk file
2013 02-07 17:24:24 UTC - 15551235566 IN - Approx Minutes: 0.1
2013 02-07 17:53:11 UTC - 15554561122 IN - Approx Minutes: NOT RECORDED
2013 02-07 17:58:28 UTC - 15554561122 OUT - Approx Minutes: 2.0
2013 02-07 22:08:05 UTC - 15554561122 OUT - Approx Minutes: NOT RECORDED
2013 02-07 22:12:04 UTC - 15551235566 OUT - Approx Minutes: NOT RECORDED
2013 02-07 22:12:55 UTC - 15551235566 OUT - Approx Minutes: 0.1
2013 02-07 22:38:43 UTC - 15558271212 OUT - Approx Minutes: 0.4
2013 02-07 23:48:36 UTC - 5552785454 OUT - Approx Minutes: 4.3
2013 02-08 00:00:26 UTC - 15559813232 IN - Approx Minutes: NOT RECORDED

所以就这样做:

ls -l /var/spool/asterisk/monitor/*.wav | awk -f tst.awk
于 2013-02-09T23:48:40.010 回答
0

尽管在您的情况下可行,但您通常应该避免解析ls. 我更希望看到一个解决方案,find而不是使用。如果您有权访问GNU awk,那么您可以大大简化您的管道。像这样运行:

awk -f script.awk <(find /var/spool/asterisk/monitor/ -maxdepth 1 -type f -name "*.wav" -printf "%p %s\n" | sort -n)

内容script.awk

BEGIN {
    t = systime()
    y = t - 60 * 60 * 24

    t = strftime("%Y-%m-%d-%H%M_UTC", t)
    y = strftime("%Y-%m-%d-%H%M", y)
}

{
    p = "^..(....)(..)(..)-(..)(..)(..)-\\+?([^-]*)-([^\\.]*).*$"
    r = "\\1 \\2-\\3 \\4:\\5:\\6 UTC - \\7 \\8 - Approx Minutes:"

    s = ($2 != 44 ? sprintf("%.1f", $2/1000000) : "NOT RECORDED")

    print gensub(p, r, "", $1) FS s > y "__" t "-call-log.txt"
}

在我的测试中,这会生成一个包含您所需输出的日志文件。因为您现在正在使用find,所以如果您的文件名开始包含空格或换行符,则可以轻松修改该方法。请让我知道情况如何。干杯。

于 2013-02-10T03:07:18.663 回答
0

你可以把所有的格式化都放到AWK里,为什么要用sed?

为了简单起见,请使用

[root@gleb monitor]# ls -l --time-style="+%Y-%m-%d %H:%M -"
-rw-r--r-- 1 asterisk asterisk    5195 2013-01-09 21:42 - 20130109-214242-1357756962.1658.WAV
-rw-r--r-- 1 asterisk asterisk   13450 2013-01-13 22:33 - 20130113-223350-1358105630.4124.WAV

不幸的是,我不能提供完整的脚本,因为我还有其他文件。基于这样的 ls 命令输出,您现在不需要重写数据,因此可以将其用作列。您可以进行完整的功能处理,包括在单个 awk 表达式中进行格式化。 http://www.gnu.org/software/gawk/manual/html_node/Printf-Examples.html

于 2013-02-08T03:13:50.937 回答