Extract 2 or more int from the text with delimiter.

Hi All,
I want to extract the integers from the each line, each line may have 2 or more integers.
The following command is appending each integers.
echo "Hi I am 100, my friend is 500, his friend is 423" | sed "s/[^0-9]//g"
100500423

I need to have delimiter "|" between the integers. If anyone knows the command, please share with me.

Thanks
Sarwan

echo "Hi I am 100, my friend is 500, his friend is 423" | sed -n "s/\([^0-9]*\)\([0-9]\+\)/\2|/gp"

echo "Hi I am 100, my friend is 500, his friend is 423" | sed -n "s/\([^0-9]*\)\([0-9]\+\)/\2|/gp"

The above command doesn't give any output. My machine is SunOs.

use nawk on Solaris

$ echo "....."  |  awk  '{for(i=1;i<=NF;i++){ gsub(/,/,"",$i);if($i+0==$i){print $i} } }'

$ echo "....." | awk '{for(i=1;i<=NF;i++){ gsub(/,/,"",$i);if($i+0==$i){print $i} } }'

The above command with awk and nawk are not working.

show what is not working, how you execute. what error messages. i (and we) am not a psychic so don't expect us to know what's happening at your side.

Please be more specific, what is the problem? Do you get errors, wrong output or no output?
Try this anyway:

echo "Hi I am 100, my friend is 500, his friend is 423" | 
awk -F"[ ,]" '{for(i=1;i<=NF;i++){if(int($i)==$i){s=s?s"|"$i:$i}}print s}'
 grep -o "[0-9]*"
echo "Hi I am 100, my friend is 500, his friend is 423" |sed -n 's/[^0-9]\+/|/gp'

this works on my machine

mo@mo-laptop:~/scripts$ echo "Hi I am 100, my friend is 500, his friend is 423" | grep -o [0-9]* | awk 'BEGIN{ ORS="|" } { print $1 } END { print "\n"}'
100|500|423|
|mo@mo-laptop:~/scripts$ 

Another sed solution :wink:

echo "Hi I am 100, my friend is 500, his friend is 423"| sed 's/[a-z A-Z]//g;s/,/|/g'
100|500|423

this one gets my vote, much cleaner than my solution

Hi Scrutinizer, the following is not giving any output in SunOS.
echo "Hi I am 100, my friend is 500, his friend is 423" |sed -n 's/[^0-9]\+/|/gp'
No outout for the above cmd.

echo "Hi I am 100, my friend is 500, his friend is 423" | grep -o [0-9]* | awk 'BEGIN{ ORS="|" } { print $1 } END { print "\n"}'
grep: illegal option -- o
Usage: grep -hblcnsviw pattern file . . .

Hi Franklin52, I modified your command little bit, and it works perfectly for one line input.
echo "update emp set emp_id='5000100' where emp_id='7894561';" | nawk -F"[ ']" '{for(i=1;i<=NF;i++){if(int($i)==$i){s=s?s"|"$i:$i}}print s}'
5000100|7894561

I have file tt.txt which has 2 lines in it.
cat tt.txt
update emp set emp_id='5000100' where emp_id='7894561';
update emp set emp_id='5000200' where emp_id='5894561';

cat tt.txt | nawk -F"[ ']" '{for(i=1;i<=NF;i++){if(int($i)==$i){s=s?s"|"$i:$i}}print s}'
5000100|7894561
5000100|7894561|5000200|5894561

Here the 1st line output keep on getting appended to the next line, it should not get appended.

Hi danmero,
I tried your approach. but I coulfn't get the final output.

cat tt.txt | sed 's/[a-z A-Z]//g' | sed 's/_=//g' | sed 's/;//g'
'5000100''7894561'
'5000200''5894561'

cat tt.txt | sed 's/[a-z A-Z]//g' | sed 's/_=//g' | sed 's/;//g' |sed 's\"''"\|\g'
sed: command garbled: s\""\|\g

[/home/sarwan]>cat tt.txt | sed 's/[a-z A-Z]//g' | sed 's/_=//g' | sed 's/;//g' | sed 's/\'\'/|/g'
>
>

Thanks
Sarwan

dude, not to sound like an ass, but maybe you should at least try and figure out how to transcribe the above code for your system. i mean, you can't expect us to write code and figure out how to make it work on your system.

the theory doesn't change, just the syntax. the tools on your OS probably differ slightly from the ones we use. go check the man pages and figure out how to change the code appropriately. if your system doesn't take the grep -o (which outputs only the pattern), check your docs and figure out what the equivalent option is for your OS.

Hi sarwan, I do not have a Solaris system handy, perhaps using

/usr/xpg4/bin/sed

or something similar could make a difference?

Empty the variable s after the print command:

nawk -F"[ ']" '{for(i=1;i<=NF;i++){if(int($i)==$i){s=s?s"|"$i:$i}}print s;s=""}' tt.txt

Hi Franklin your code works perfectly, So much thanks to you and others who are all replied to my post.

Momo.reino, really last two days I am stuck with many issues, I really sorry for not looking into the manual for that particular cmd.

echo "Hi I am 100, my friend is 500, his friend is 423" | perl -ne '{my @tmp=$_=~/([0-9]+)/g;print join "|" ,@tmp;}'

One with tr and xargs...

echo "Hi I am 100, my friend is 500, his friend is 423" | tr -c '[0-9]' ' ' | xargs | tr ' ' '|'