![grep 日志文件并将其写入 csv 文件](https://linux22.com/image/181276/grep%20%E6%97%A5%E5%BF%97%E6%96%87%E4%BB%B6%E5%B9%B6%E5%B0%86%E5%85%B6%E5%86%99%E5%85%A5%20csv%20%E6%96%87%E4%BB%B6.png)
我根据用户的呼叫 ID 通过播放给用户的编解码器文件执行 grep 操作。向用户/呼叫 ID 播放了 2 个文件。我正在将这些 grep 输出写入 .csv 文件。我用了
grep -oP "(?<=$START).*?(?=$END)" $IVRLOG$Progress_log>>$CURRENTPATH/op2.csv
welcome1.g711u #(call_id1)
Announcementbui4.g711u #(call_id1)
welcome1.g711u #(call_id2)
Announcementbui4.g711u #(call_id2)
但我想要输出为
welcome1.g711u,Announcementbui4.g711u #callid1
welcome1.g711u,Announcementbui4.g711u #callid2
答案1
grep -oP "(?<=$START).*?(?=$END)" $IVRLOG$Progress_log>>$CURRENTPATH/op2.csv |
while read line; do
grep "callid" | sed 's/()()//g' #removing the brackets
done
您可能仍然需要处理新空间代码未测试
答案2
如果 grep -oP“(?<=$START)。?(?=$END)" $IVRLOG$Progress_log; 然后 echo "找到匹配项" { printf "%s," "$target_users"; grep -oP "(?<=$START).?(?=$END)" $IVRLOG$Progress_log | sed '$!N;s/\n/,/'; }>>$CURRENTPATH/E41.csv else echo "未找到匹配" { printf "%s\ n" "$target_users"; }>>$CURRENTPATH/E41.csv fi 完成 我通过针对 call_id 迭代上述代码获得了输出。我得到的所需输出:9188888881,welcome1.g711u,Announcementbui4.g711u 9177777772,欢迎1.g711u,公告bui4.g711u 9166666663,欢迎1.g711u,公告bui4.g711u 9155555554,欢迎1.g711u,公告bui4.g711u 9144444445,欢迎1.g711u,公告bui4.g7 11u Haxiel 在这方面帮助了我。