Skip to content

Instantly share code, notes, and snippets.

@dshipp
Created October 9, 2013 11:02
Show Gist options
  • Save dshipp/6899532 to your computer and use it in GitHub Desktop.
Save dshipp/6899532 to your computer and use it in GitHub Desktop.
Use sed to match a regular expression and replace the full line with just the match.
#!/bin/bash
# Use \( and \) to create a match group in the find part, use \1 in the replace part to reference the match group.
echo "./ThingToMatch/other/stuff/" | sed -n 's/\.\/\([a-zA-Z0-9]*\)\/.*/\1/p'
# This will output: ThingToMatch
# Works with multiline input, applying the replacement to each line.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment