tags:

views:

322

answers:

5

I'm trying to parse various info from log files, some of which is placed within square brackets. For example:

Tue, 06 Nov 2007 10:04:11 INFO     processor:receive: [someuserid], [somemessage]  msgtype=[T]

What's an elegant way to grab 'someuserid' from these lines, using sed, awk, or other unix utility?

+7  A: 

cut
use it like this: cut -f2 -d[ | cut -f1 -d]

bart@hal9k:~> YOURTEXT="Tue, 06 Nov 2007 10:04:11 INFO     processor:receive: [someuserid], [somemessage]  msgtype=[T]"
bart@hal9k:~> SOMEID=`echo $YOURTEXT | cut -f2 -d[ | cut -f1 -d]`
bart@hal9k:~> echo $SOMEID
someuserid
vartec
Nice, I wasn't aware of cut. Works beautifully.
Parand
yeah, it so basic, yet ppl forget about it's existence.
vartec
A: 

If you want to do something with all the bracketed fields, I'd use Perl:

perl -lne '
    my @fields = /\[(.*?)\]/g;
    # do something with @fields, like:
    print join(":", @fields);
' logfile ...
glenn jackman
A: 

using bash shell

while read -r line
do
   case "$line" in
        *processor*receive* ) 
            t=${line#*[}
            echo ${t%%]*}
            ;;
   esac
done < "file"
A: 
sed -n '/INFO/{s/.[^[]*\[//;s/\].*//p}' file
A: 

Using AWK:

cat file | awk -F[\]\[] '{print $2}'

I have found that multiple delimiters do not work in some older versions of AWK. If it doesn't, you can use two awks:

cat file | awk -F[ '{print $2}' | awk -F] '{print $1}'
dogbane