2013-02-08 66 views
0

我知道星號會以csv文件的形式創建自己的通話記錄。爲了我的目的,我需要格式化的通話記錄,如下所示。我使用:縮短sed命令 - 使用它製作星號通話記錄

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 

回答

0

您可以將所有格式化爲AWK,爲什麼使用sed?

要得到它簡單,使用

[[email protected] 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 

Unfortanly我不能給完整劇本,監守我有其他文件。基於這樣的ls命令輸出,您現在不需要重寫數據,因此可以將它用作列。 您可以執行全功能處理,包括在單個awk表達式中格式化。 http://www.gnu.org/software/gawk/manual/html_node/Printf-Examples.html

+0

我對sed更加熟悉,但如果AWK更乾淨,我會提出建議。 – errorcode

+0

但awk只允許以正確的格式打印。單個printf調用。 – arheops

+0

這個很好用,謝謝! – errorcode

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 
0

雖然你的情況是可行的,你應儘量避免parsing ls。我寧願看到使用find的解決方案。如果您有權訪問GNU awk,則可以大大簡化管道。運行,如:中script.awk

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

內容:

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,所以如果您的文件名開始包含空格或換行符,則可以輕鬆修改該方法。請讓我知道它是怎麼回事。乾杯。