I have a string 36K total,TOO_MANY_NEWLINE_IN_TEXT_tABLE. From this string i need only 36.
is that possible?
As described in https://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion, you can write
echo "${foo:0:2}"
to output the first two characters of a variable foo.
You could also try this:
echo "36K total,TOO_MANY_NEWLINE_IN_TEXT_tABLE" | grep -oEi '([0-9])+'
-i flag?To extract the leading number (of variable length) from a string in pure bash (without any external tools), you can use the =~ operator (extended regex match) inside the conditional expression [[ .. ]]:
$ line='36K total,TOO_MANY_NEWLINE_IN_TEXT_tABLE'
$ [[ $line =~ [0-9]* ]] && echo "$BASH_REMATCH"
36
echo '36K total,TOO_MANY_NEWLINE_IN_TEXT_tABLE' | sed 's/[^0-9].*//'?