Quantcast
Channel: How can I extract tag from syslog message, and us it as path variable? - Server Fault
Viewing all articles
Browse latest Browse all 3

Answer by Scottie H for How can I extract tag from syslog message, and us it as path variable?

$
0
0

BLUF: Yes, it is.
Q: If you know the tag already, why do you need to extract it?
Posit 1: I know MY_SPECIFIC_TAG and want to extract it from the messages file:
grep MY_SPECIFC_TAG /var/log/messages | sed -n "1s/^[A-Z][a-z][a-z] [0-9:.]* (.): ./\1/p"
That ought to get you MY_SPECIFIC_TAG (or at least pretty close). YMMV
Posit 2: I know MY_SPECIFIC_TAG but I do not know the path in front of it (i.e. /path/to/program/MY_SPECIFIC_TAG)
grep MY_SPECIFC_TAG /var/log/messages | sed -n "1s+^[A-Z][a-z][a-z] [0-9:.]* (/[a-zA-Z0-9/]/.): .*+\1+p"

Is that what you are looking for?
To set that as a variable, place the entire command inside $() and use that as the right side of the assignment.
my_path_var=$( {command from above} )

UPDATE:If you don't know what MY_SPECIFIC_TAG is, and you want to find them:
cat /var/log/messages | sed -n "s/^[A-Z][a-z][a-z] [0-9:.]* (.): ./\1/p" | sort -u
This will not print any lines without a tag.

Now, before you go all "UUOC!" on me, I've done it this way to show the comparison of this command to the old command. That way, the OP (and other future readers) can easily tailor these commands to their system.
Note: UUOC = Unnecessary Use Of Cat. The file name can be placed after the p" and the cat command removed. However, for demonstrative purposes, cat is being used here.


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>