Friday, 15 April 2011

shell - Store into variable a string that changes dynamically -



shell - Store into variable a string that changes dynamically -

i'm quite new unix in shell scripting assume need help regarding next issue:

i want store variable string of log text changes dynamically. e.x 1 time run programme creates log, string may server1 , next time server238. have found ways find first occurrence of string through sed or grep , cut. since log file software creates may differ version version can't count on specific line contains string. e.x 1 version may log "the server using server98" , next 1 "used server server98". there way through sed or awk string may retrieved regardless of log layout. in advance.

i'd go with:

server=$(grep -eo 'server[0-9]+' file | head -n 1)

to find occurrence of word server followed digits, e.g. server3, server98

-e means utilize extended regular expressions (i.e. \d+ multiple digits)

-o means output matching part of string - not whole line contains it.

here in action on osx:

cat file server9 fred server98 fred 3 /usr/bin/grep -eo 'server[0-9]+' file server9 server98

shell awk sed

No comments:

Post a Comment