1

I have a few strings like this:

var1="string one=3423423 and something which i don't care"
var2="another bigger string=413145 and something which i don't care"
var3="the longest string ever=23442 and something which i don't care"

These strings are the output of a python script (which i am not allowed to touch), and I need a way to extract the 1st part of the string, right after the number. Basically, my outputs should be:

"string one=3423423"
"another bigger string=413145"
"the longest string ever=23442"

As you can see, i can't use positions, or stuff like that, because the number and the string length are not always the same. I assume i would need to use a regex or something, but i don't really understand regexes. Can you please help with a command or something which can do this?

3 Answers 3

1
grep -oP '^.*?=\d+' inputfile
string one=3423423
another bigger string=413145
the longest string ever=23442

Here -o flag will enable grep to print only matching part and -p will enable perl regex in grep. Here \d+ means one or more digit. So, ^.*?=\d+ means print from start of the line till you find last digit (first match).

Sign up to request clarification or add additional context in comments.

2 Comments

You should probably use a non-greedy form of your RE (^.*?=\d+) to fully comply with the OP's requirements.
@PS. I have made a mistake in the original post: Basically i have var="string one=3423423 and something which i don't care" and i need a command, that turns that string into string one=3423423
0

You could use parameter expansion, for example:

var1="string one=3423423 and something which i don't care"
name=${var1%%=*}
value=${var1#*=}
value=${value%%[^0-9]*}
echo "$name=$value"
# prints: string one=3423423

Explanation of ${var1%%=*}:

  • %% - remove the longest matching suffix
  • = - match =
  • * - match everything

Explanation of ${var1#*=}:

  • # - remove the shortest matching prefix
  • * - match everything
  • = - match =

Explanation of ${value%%[^0-9]*}:

  • %% - remove the longest matching suffix
  • [^0-9] - match any non-digit
  • * - match everything

To perform the same thing on more than one values easily, you could wrap this logic into a function:

extract_and_print() {
    local input=$1
    local name=${input%%=*}
    local value=${input#*=}
    value=${value%%[^0-9]*}
    echo "$name=$value"
}

extract_and_print "$var1"
extract_and_print "$var2"
extract_and_print "$var3"

7 Comments

this works if my string would be string1="3423423 and something which i don't care" but my string is var="string one=3423423 and something which i don't care" Basically, the = is inside the string, it is not the sign for assigning something to a variable. My bad, i have to edit the 1st post :)
I am not used with asking questions, and for a new line it seems i have to press Shift + Enter instead of just Enter. Please recheck my comment, i've edited it.
i really am sorry for the mistake. This is my 2nd time i'm asking for help (usually i can make it on my own), and plus the fact that this is the 3rd day i'm working in Bash, so it's a bit difficult for me to ask good questions, cuz basically i don't really know what to ask :)
@DavidBotezatu I rewrote my answer according to match your updated question.
Thank you very much! I still have 1 more question for you: why did you do value=${var1#*=} value=${value%%[^0-9]*} instead of maybe value=${var1#*=} value2=${value%%[^0-9]*} Can you please explain? Thanks!
|
0
$ shopt -s extglob

$ echo "${var1%%+([^0-9])}"
string one=3423423

$ echo "${var2%%+([^0-9])}"
another bigger string=413145

$ echo "${var3%%+([^0-9])}"
the longest string ever=23442

+([^0-9]) is an extended pattern that matches one or more non-digits.
${var%%+([^0-9])} with %%pattern will remove the longest match of that pattern from the end of the variable value.

Refs: patterns, parameter substitution

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.