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
    Jan 11, 2016 at 15:18
  • Your input and output make no sense...
    – 123
    Jan 11, 2016 at 15:43
  • Thank you for replying that's true i made mistake, now it's corrected.
    – npower
    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? Jan 18, 2016 at 16:34
  • @BenjaminW. You are right. That is not necessary. Jan 19, 2016 at 1:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.