Code Monkey home page Code Monkey logo

strans's Introduction

strans

Build Status LOC Github Releases Join the chat at https://gitter.im/inventitech/strans

strans (string transform) is an intuitive string manipulation utility for the shell (primarily Unix, but should work™ cross-platform). The user does not need to know any programming. All she needs to do is provide strans with a set of examples. strans will automagically learn transformation rules from these examples and apply them to the input given on STDIN.

How to Install

strans is distributed as an AppImage for Linux and as a DMG for MacOs. Download the latest strans from releases.

Linux

Our AppImage should run on practically any recent linux Desktop distribution.

After downloading, simply do

chmod +x strans-linux.AppImage ./strans-linux.AppImage

To install it system-wide as strans, just

sudo cp strans-linux.AppImage /usr/bin/strans

MacOs

After mounting the dmg and copying strans.app into /Applications, it might be necessary to

chmod +x /Applications/strans.app/Contents/MacOS/strans
/Applications/strans.app/Contents/MacOS/strans

to run strans.

How to Use

# With before and after example
strans -b pattern-to-match -a desired-transformation

# With file that contains examples
strans -f file-with-examples

# Help page
strans --help

Examples

Strans in action

Example 1: Extract ending of files

Assume that

ls
Document.pdf  Document2.pdf Document.txt  Document.png

Now we want to get a unique list of all file endings present in the directory:

ls | strans -b Document.pdf -a pdf | sort -u

Note how nicely strans (here defined as an alias) integrates with other tools.

Of course, as StackOverflow will tell you, we could obtain the same result with

ls | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u 

But with strans we accomplished the same with much less brain work, without StackOverflow and Perl, but instead with pure joy!

Example 2: Convert full names to their initials

printf "Moritz Beller\nGeorgios Gousios" |
strans -b "First Last" -a "FL"

neatly outputs

MB
GG

However, when we add a third entry with a middle name, Andy Emil Zaidman, things start to break, as this does not appear in the initials:

MB
GG
AZ

We can fix this by providing strans with another example. We create a file called example-transformations

First Last => FL
Firstname Middlename Lastname => FML

and call

printf "Moritz Beller\nGeorgios Gousios\nAndy Emil Zaidman" |
strans --example-file example-transformations

And, voila, the output is

MB
GG
AEZ

Note how strans adds the second example and generates a global transformation rule that satisfies all examples given to it. Simply having the last FML example would not be enough, because it would miss the case where only two names are available.

How to Develop

You need dotnet to run strans.

git clone https://github.com/Inventitech/strans.git
cd strans
dotnet restore
dotnet publish -c Release

An alias (in your bashrc, ...) can make strans integrate seamlessly in a Unix environment:

alias strans="dotnet path/to/strans.dll"

Background

strans uses program-by-example techniques from Microsoft PROSE to come up with the rules behind this string manipulation. PROSE allows the creation of extremely complex string transformations within a matter of a few seconds by just giving easy-to-write examples. In its essence, strans is only a light-weight wrapper around and direct application of Microsoft's PROSE framework. strans provides the goodness of the now-removed PowerShell (!) command Convert-String.

strans's People

Contributors

inventitech avatar mfragkoulis avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.