[aklug] Re: sed regex help needed

From: Kenneth D Weinert <kenw@quarter-flash.com>
Date: Tue Nov 17 2009 - 09:34:42 AKST

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Kevin Miller wrote:
> Kenneth D Weinert wrote:
>> Lee wrote:
>>> heya folks,
>>>
>>> Been working on this for a while now, and decided it's time to call for assistance.
>>>
>>> I'm trying to use sed to extract an arbitrary length substring that begins with KB and
>>> ends with - . That's a space-dash.
>> Does it have to be sed?
>>
>> echo "[44]: KB951072-v2 - Update"|cut -d' ' -f2
>>
>> works as well and is less complicated.
>
> Cool. I'm not the OP, but will tuck that pearl away for future
> reference. Been many a time I could have used it!
>
> FWIW, with a quick look at the cut man page and a bit of playing I was
> able to see that if one wants the ' -' tacked on the end of the
> extracted string, just change the -f2 to -f3
>
> ...Kevin

Well, actually, you'd want to change -f2 to -f2-3 to get both fields. In
this case you could also use -f2,3 (the first is a range of fields, the
second is selected fields.)

The cut command is a nice way to extract fields from a directory listing
as well, but since cut treats each separator as a separator (that is, a
set of X adjacent separators are treated as X fields, not 1 field) the
trick is to run the output of ls through a tr -s ' ' first:

ls -l | tr -s ' ' | cut -d' ' -f5,8-

will list the file size and name of the files in your directory (you may
need to adjust the field numbers if your ls is different.)

the 8- is to account for file names with embedded spaces.

Sorry for the cli lesson here, but getting info from an ls is a somewhat
common occurrence and it can be just a tiny bit frustrating when you
first try it :) Don't ask me how I know. :)

- --
Ken
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAksC7LwACgkQH0OpnUzq8fDZYQCgky4+e32EGAzdXKNjjpjOSa1f
cVgAoKQ+eIpfRdxYe1ZLGS5EDHNCdz4t
=+6MW
-----END PGP SIGNATURE-----
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Tue Nov 17 09:35:22 2009

This archive was generated by hypermail 2.1.8 : Tue Nov 17 2009 - 09:35:22 AKST