Walt Mankowski on 9 Nov 2009 11:47:20 -0800

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] extended 'cut' functionality?

On Mon, Nov 09, 2009 at 02:16:16PM -0500, Randall A Sindlinger wrote:
> Hi folks,
> Quick (stupid) question.
> The 'cut' utility will let me do trivial string manipulation counting
> from the beginning of a string.
> Is there anything that provides similar, but extended, functionality to
> count from the _end_ of a string?
> Specifically, I'd like to be able to 
>   cd `perldoc -l Some::module | cut-from-end 3`
> Sure, I know there are other things I could do, like doing
>   cut -d . -f 1
> or an awk thing, but both of those just don't feel "right".  It should
> be trivial to snip off the end of a string from the command line.
> I'd google it, but I can't imagine the crud I'd have to wade through.

It's easy to do it with a perl one-liner:

$ perldoc -l LWP::Simple

$ perldoc -l LWP::Simple | perl -lne 'print substr($_,0,-3)'

You could also make it a bash function like this:

function cut-from-end {
  perl -nle 'print substr ($_,0, -'$1')'

Attachment: signature.asc
Description: Digital signature

Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug