1

I am writing simple bash script (actually learning how to do it ) and i want this function to use just echo and sed and return exactly the same output. I found solutions with sed, they worked in console but did not work inside of script. (script is called with sudo, maybe it's important)

This is my function:

function strip
{
     echo "$1" | grep -oP '[a-zA-Z0-9\+\-\=\ ]+' | head -n 1;
}

This is how it is suppsed to work

Input: "-Wall"
Output: "-Wall"

Input: "-O3%## -Wall"
Output: "-O3"

Input: "%#$#$"
Output: ""

Can anyone show how would it look like using sed?

3
  • 1
    Show input and expected output plz.
    – 123
    Commented Jan 11, 2016 at 15:18
  • Your input and output make no sense...
    – 123
    Commented Jan 11, 2016 at 15:43
  • Thank you for replying that's true i made mistake, now it's corrected.
    – npower
    Commented Jan 11, 2016 at 15:45

1 Answer 1

2
echo ___abc___ | sed 's/^.*\(abc\).*$/\1/'
abc
2
  • 1
    Why even capture the groups you're not using? Commented Jan 18, 2016 at 16:34
  • @BenjaminW. You are right. That is not necessary. Commented Jan 19, 2016 at 1:04

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.