This thread pretty much answered for the question I had in mind while browsing these columns and vgersh99 nawk answer worked for me the best, to get rid of the trailing blanks or spaces.
So I would like to add an extra question:
I have an a error.log file where error messages from the message system queue are written into the same line.
So only one long line in a error.log file containing multiple error messages.
Those error messages are separated by whitespaces or tabs etc. from each other.
So it is one big chunk of text and the log file is not fixed length. Therefore using $1, $2 won't work eventhough there might be common phrase which to grep.
What I would like to get out of that log file is the last error message and send it to my email, to see what is going on in a system if error occurs.
In n a script level, after removing the spaces from the end
a) is to take the last, lets say 150 characters from the the end of the file and put them into another variable
or
b) tougher, preferred one: Take the last message entirely not considering how long it is and put it in a variable
Example from the (imaginary) error.log
(sorry, couldn't attach here the original one)
------
ERROR occured in system (here lotsa spaces & tabs)
ANOTHER ERROR occurred (again lotsa spaces)
AGAIN something happened (spaces, tabs) <-File ends to spaces, tabs, what ever.
From the end these spaces etc. blanks can be eliminated with the vgersh99 nawk help.
ER=error.log
cat $ER| awk 'sub(/[ \t]*$/, "")' > /tmp/error.txt
So in the error.txt file it has removed from the end the white spaces and tabs, but how to get the last message:
AGAIN something happened
into variable MSG so that I could email it
MSG=$(cat /tmp/error.txt)
mailx -s "Error occured in a system" $MSG my.name@domain.com
and with awk this could be simplified w/o using temporary files, but I am not that familiar with it.
Something like this
awk 'sub(/[ \t]*$/, "")' $ER (works)
MSG=$(awk 'sub(/[ \t]*$/, "")' $ER) (insert into variable doesn't work)
Plus addition to here to grep the last error message.
Here could be used previous message white spaces as a separator since in a actual error.log message file there are normally more than 4 spaces between messages, but single message could contain 2-3 spaces.
So to play safe 4 or more spaces could be used as a separator and the end of the file.
Is it the EOF in awk NF or what does that mean in awk syntax?
Regards,
#Kimmers -beginner awker =bawker? )
So I would like to add an extra question:
I have an a error.log file where error messages from the message system queue are written into the same line.
So only one long line in a error.log file containing multiple error messages.
Those error messages are separated by whitespaces or tabs etc. from each other.
So it is one big chunk of text and the log file is not fixed length. Therefore using $1, $2 won't work eventhough there might be common phrase which to grep.
What I would like to get out of that log file is the last error message and send it to my email, to see what is going on in a system if error occurs.
In n a script level, after removing the spaces from the end
a) is to take the last, lets say 150 characters from the the end of the file and put them into another variable
or
b) tougher, preferred one: Take the last message entirely not considering how long it is and put it in a variable
Example from the (imaginary) error.log
(sorry, couldn't attach here the original one)
------
ERROR occured in system (here lotsa spaces & tabs)
ANOTHER ERROR occurred (again lotsa spaces)
AGAIN something happened (spaces, tabs) <-File ends to spaces, tabs, what ever.
From the end these spaces etc. blanks can be eliminated with the vgersh99 nawk help.
ER=error.log
cat $ER| awk 'sub(/[ \t]*$/, "")' > /tmp/error.txt
So in the error.txt file it has removed from the end the white spaces and tabs, but how to get the last message:
AGAIN something happened
into variable MSG so that I could email it
MSG=$(cat /tmp/error.txt)
mailx -s "Error occured in a system" $MSG my.name@domain.com
and with awk this could be simplified w/o using temporary files, but I am not that familiar with it.
Something like this
awk 'sub(/[ \t]*$/, "")' $ER (works)
MSG=$(awk 'sub(/[ \t]*$/, "")' $ER) (insert into variable doesn't work)
Plus addition to here to grep the last error message.
Here could be used previous message white spaces as a separator since in a actual error.log message file there are normally more than 4 spaces between messages, but single message could contain 2-3 spaces.
So to play safe 4 or more spaces could be used as a separator and the end of the file.
Is it the EOF in awk NF or what does that mean in awk syntax?
Regards,
#Kimmers -beginner awker =bawker? )