Extract number from string.

Hi

I am on Sun os. I have data in the below format and I need to grab the number out from the string.

O/p needed:

---------- Post updated at 12:39 PM ---------- Previous update was at 12:32 PM ----------

I tried this but I am getting . at the front

Hello dsravanam,

Could you please try following and let me know if this helps.

echo "Insert completed. 100 rows added" | awk '{gsub(/[^[0-9]]*/,"");print}'
OR
echo "Insert completed. 100 rows added" | awk '{print $(NF-2)}'
OR
echo "Insert completed. 100 rows added" | awk '{gsub(/[[:alpha:]]|[[:punct:]]| +/,"");print}'

Thanks,
R. Singh

echo 'Insert completed. 100 rows added ' | awk '{print $(NF-2)}'

So - punctuation chars and space remain? Where did you get the [:alpha:] character class?

works fine with integer :

$ echo "Insert completed. 100 rows added" | tr -dc '[:digit:]'
100
1 Like

I think none of them are working fine.

echo "Insert completed. 100 rows added" | tr -dc '[:digit:]'

gives

Insert completed. rows added

The other one below is not working as it is bringing characters when the string is like below

echo 'Insert completed. No rows added ' | awk '{print $(NF-2)}'
No

Assuming you are using bash...
Longhand, OSX 10.12.3, default bash terminal:-

Last login: Tue Feb 28 20:19:10 on ttys000
AMIGA:amiga~> ARRAY=( Insert completed. 100 rows added )
AMIGA:amiga~> echo "${ARRAY[2]}"
100
AMIGA:amiga~> _

This assumes that there is always an integer value as the third field.

typeset -i num=$(echo 'Insert completed. No rows added ' | awk '{print $(NF-2)}')

Using the form ... | tr -dc '[:digit:]' works fine for me. The definition for the -c flag is for the complement, i.e. the opposite so it should delete everything except digits.

Can you check your manual page to see if the ic flag is supported?

If you are in bash and the message is always the same format, you could also do this:-

read a b rows c < <(echo "Insert completed. 100 rows added.")
echo $rows

It only calls echo so avoids the extra process for tr, awk or whatever and doesn't care if you have digits or the text No

You can replace the echo command with whatever command you need to generate the output in the first place.

Does this help?

Robin

Sed alternative:

echo "Insert completed. 100 rows added" | sed 's/\([^[0-9]*\)\([0-9]*\)\(.*\)/\2/'

Hi.

Also:

echo "Insert completed. 100 rows added"|sed 's/[^0-9]//g'

producing:

100

On a system:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
sed (GNU sed) 4.2.2

Best wishes ... cheers, drl

Try

print "Insert completed. 100 rows added" | sed 's/[^0-9]//g'

bash / ksh93 / zsh:

str='Insert completed. 100 rows added'
echo "${str//[!0-9]}"

This also works with recent versions of bash (at least GNU bash, version 3.2.57 and later; but I don't know how far back it appeared in earlier releases).

This form of variable substitutions is an extension that is allowed, but neither required nor explicitly mentioned, by the POSIX standards.

1 Like

:slight_smile: I meant to write bash , but somehow the "b" did not make it to the post :),
This form of parameter expansion was introduced in GNU bash version 2.0 (Nov 1996).

--
(the typo suggested that the Almquist shell ( ash ) could do it, which is not the case, nor is it the ambition of ash ..)

1 Like