On Fri, Apr 05, 2002 at 05:03:24PM +1200, Wayne Rooney wrote:
> From: Nick Rout <[EMAIL PROTECTED]>
> >I have  a string like postfix/smtpd[25532]: I want to cut it off at the
> >[ so I end up with postfix/smtpd 
> >
> For a fixed length string try
> 
> echo postfix/smtpd[25532] | cut -c 1-13

Actually, cut is much more flexible than that, and if you're running it
over large data sets, it's faster than an equivalent sed.

In this instance you want to use the invocation 'cut -d\[ -f1'
The -d option (with a quoted [, in case your shell thinks you're trying
to introduce a real metacharacter) tells cut to parse the input into a
number of columns, with [ acting as the delimeter.
-f1 selects the first column. The [ character will not appear in any of
the output fields ... it's the delimeter, not data.
Other -f options include things like -f2-3 for columns 2 and three,
-f3,5 for columns 3 and 5, and the useful -f2- for al columns from 2
onwards.

But really, just getting the postfix/smtpd section isn't that useful -
it doesn't change. Perhaps you want to identify the postfix entries in
the log file, and collect their values?

$ fgrep postfix maillog | cut -d: -f2-

Given an input file of
things [234]: This is a message
postfix [266]: This is another message
somethings [456]: This is a useless message
thongs [567]: This is not a message
swings [678]: This is an interesting message
postfix/smtpd [789]: This is because of an email message

You'll get
 This is another message
 This is because of an email message

(fgrep, or 'frep -F', is faster if you're searching for a fixed string,
as opposed to a regexp)

-jim

Reply via email to