缩短sed命令 - 使用它来制作星号呼叫日志

时间:2013-02-08 03:02:56

标签: sed asterisk

我知道星号会以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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分:0)

虽然您的情况可行但通常应避免使用parsing 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,所以如果您的文件名开始包含空格或换行符,则可以轻松修改该方法。请告诉我它是怎么回事。欢呼声。