2014-10-19 4 views
1

터미널에 인쇄 할 다채로운 선을 만들기 위해 노력하고 있습니다.프로그래밍 방식으로 문자열을 인쇄하십시오.

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 

내 출력은 별도의 줄에 인쇄되고, 그래서 다음과 같습니다

Current terminal output

그러나, 나는 각 오류를하고자 한 줄에 인쇄 할, 같은 :

desired output

어떻게하면됩니까?


일부 우수 답변을받은 후, 나는 다음과 같이 내 스크립트를 리팩토링 :

#!/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 
+0

당신은'printf와 "$ var에"를 '사용하지 말아야합니다. 대신에'printf '% s' "$ var"'를 사용하십시오. '$ var'에 퍼센트 기호가 포함되거나 하이픈으로 시작되는 경우 이것으로 많은 두통을 줄일 수 있습니다. –

답변

1

문제는 cut 명령 출력의 끝에 줄 바꿈 \n 문자를 출력합니다. 이를 극복하기

한가지 방법은 어떤 명령 대체 기술

$printf "hello world" | cut -f1 
hello world 
$ 

output=`printf "hello world" | cut -f1` 
printf $output 
hello world$ 

출력 끝에 배시 프롬프트 $ 참조

로 변형 될 수있다 예를 들어

를 사용하는 hello world

1

최종 c 이 풀 라인 출력이 때문에 라인

printf "${LINE:5}" | cut -d "[" -f2 | cut -d "]" -f1 

에서 모든 cut는 개행을 생성한다. 대신, 당신은 read를 사용하여이 방법으로 IFS를 설정하여 cut을 피할 수

code=${LINE:5}; code=${code%*}; printf "$code"; 
+1

'printf "% s" "$ code"'를 쓰는 것이 더 안전합니다.마지막으로'code = $ {LINE : 5}; printf "% s" "$ {code % *}" –

1

를 사용

LINE="WARN Client insertion failed: [149] Validation failed: Email is invalid" 

case "$LINE" in 
    WARN*) 
    IFS="[]" read error number reason <<< "$(echo "${LINE:5}")" 
    # echo "$error | $number | $reason" 
    tput setaf 1 
    printf "%s" "$error" 
    tput sgr0 
    # printf ": " 
    tput setaf 3 
    printf "[" 
    tput sgr0 
    printf "%s" "$number" 
    tput setaf 3 
    printf "]" 
    tput sgr0 
    printf "%s" "$reason" 
    printf "\n" 
    ;; 
esac 
+0

정말 훌륭합니다. 고마워. 나는 미래의 독자를 위해 내가 어떻게 리팩토링했는지에 대한 나의 질문을 업데이트 할 것이다. –

+0

나는이 트릭에서 너무 많은 마일리지를 얻고있다. 누군가 내가 맥주를 사람들에게 보낼 수있게 해주는 SaaS를 설치했다. –

관련 문제