Как постоянно отслеживать журнал, находить все файлы (sed) и отображать (cat) найденные файлы

Вы можете выполнить задачу с помощью цикла, nl ( n umbering l ine ) и sed ( s tring e ditor)

for f in scenario*.csv
do
    #next will numerate all lines exept first (started without number) 
    nl -bp^[0-9] -nln -w1 "$f" | 
    sed '
        #add the «p» before line number 
        s/^[0-9]/p&/
        #put «pNUM» on the place of second field started with «NUM-NUM»
        s/\(^p[0-9]*\)\s*\([0-9]*,\s*\)[0-9]-[0-9][^,]*/\2\1/
        #removes spaces from the line begining (may be for header only)
        s/^\s*//
        ' > out.tmp #outputs changed lines into temporary file
    mv out.tmp "$f" #move temp file to original 
done
rm out.tmp #delete temp file
1
09.05.2018, 08:14
2 ответа

Буферизация мешает.

Используйте while read lineиз оболочки, которая должна читать строку -за строкой -и избежать большинства проблем с буферизацией:

tail -f /var/log/httpd/modsec_audit.log | while read line; do
    echo "$line" | sed 's/[^\/]*/\./;s/].*$//g' | awk '{print $0}' | xargs cat
done

Хотя вы, вероятно, можете сделать лучше, используя оболочку (bash ), чтобы также сопоставить имя файла из строки журнала:

tail -f /var/log/httpd/modsec_audit.log | while read line; do
    line=${line/*([^\/])/.}
    line=${line%]*}
    [[ -n "${line}" ]] && cat "${line}"
done
3
27.01.2020, 23:32

попробуй с этим:

tail -f /var/log/httpd/modsec_audit.log | stdbuf -oL sed 's/[^\/]*/\./;s/].*$//g' | stdbuf -oL awk '{print $0}' | while IFS='' read -r file; do cat $file ; done
1
27.01.2020, 23:32

Теги

Похожие вопросы