터미널에 인쇄 할 다채로운 선을 만들기 위해 노력하고 있습니다.프로그래밍 방식으로 문자열을 인쇄하십시오.
Ruby로 파일에 내 메시지를 쓰고 있습니다. 로그 파일에 기록
def warn(line)
log = File.open("#{File.dirname(__FILE__)}/../../log/import/import.log", "a")
log.puts("WARN #{line}")
log.close
end
전형적인은 다음과 같다 :
WARN Client insertion failed: [149] Validation failed: Email is invalid
을 내가 계속 tail
으로이 파일에서 읽고 있어요 :
tail -F -n 0 log/import/import.log | script/report.sh
난 스크립트를 tail
출력을 다음과 같이 바꿉니다.
#!/bin/bash
set -e
while read LINE; do
case "$LINE" in
"file truncated")
clear
;;
INFO*)
tput setaf 6
echo "${LINE:5}"
tput sgr0
;;
WARN*)
tput setaf 1
printf "${LINE:5}" | cut -d ":" -f1
tput sgr0
printf ": "
tput setaf 3
printf "["
tput sgr0
printf "${LINE:5}" | cut -d "[" -f2 | cut -d "]" -f1
tput setaf 3
printf "]"
tput sgr0
printf "${LINE:5}" | cut -d "]" -f2
printf "\n"
;;
*)
echo "$LINE"
;;
esac
done
내 출력은 별도의 줄에 인쇄되고, 그래서 다음과 같습니다
그러나, 나는 각 오류를하고자 한 줄에 인쇄 할, 같은 :
어떻게하면됩니까?
일부 우수 답변을받은 후, 나는 다음과 같이 내 스크립트를 리팩토링 :
#!/bin/bash
set -e
printfg() {
tput setaf $3
printf "$1" "$2"
tput sgr0
}
while read LINE; do
case "$LINE" in
"file truncated")
clear
;;
INFO*)
printfg "%s\n" "${LINE:5}" 6
;;
WARN*)
IFS="[]" read error number reason <<< "$(echo "${LINE:5}")"
printfg "%s" "$error" 1
printfg "%s" "[" 3
printf "%s" "$number"
printfg "%s" "]" 3
printf "%s\n" "$reason"
;;
*)
echo "$LINE"
;;
esac
done
당신은'printf와 "$ var에"를 '사용하지 말아야합니다. 대신에'printf '% s' "$ var"'를 사용하십시오. '$ var'에 퍼센트 기호가 포함되거나 하이픈으로 시작되는 경우 이것으로 많은 두통을 줄일 수 있습니다. –