Extract 2 or more int from the text with delimiter.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract 2 or more int from the text with delimiter.
# 1  
Old 11-16-2009
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
# 2  
Old 11-16-2009
Code:
echo "Hi I am 100, my friend is 500, his friend is 423" | sed -n "s/\([^0-9]*\)\([0-9]\+\)/\2|/gp"

# 3  
Old 11-16-2009
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.
# 4  
Old 11-16-2009
use nawk on Solaris
Code:
$ echo "....."  |  awk  '{for(i=1;i<=NF;i++){ gsub(/,/,"",$i);if($i+0==$i){print $i} } }'

# 5  
Old 11-16-2009
$ 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.
# 6  
Old 11-16-2009
Quote:
Originally Posted by sarwan
$ 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.
# 7  
Old 11-16-2009
Please be more specific, what is the problem? Do you get errors, wrong output or no output?
Try this anyway:

Code:
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}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract multiple columns base on double quotes as delimiter

Hi All, I have my data like below "1","abc,db","hac,aron","4","5" Now I need to extract 1,2,4th columns Output should be like "1",abc,db","4" Am trying to use cut command but not able to get the results. Thanks in advance. (4 Replies)
Discussion started by: weknowd
4 Replies

2. Shell Programming and Scripting

Skip the delimiter with in double quotes and count the number of delimiters during data extract

Hi All, I'm stuck-up in finding a way to skip the delimiter which come within double quotes using awk or any other better option. can someone please help me out. Below are the details: Delimited: | Sample data: 742433154|"SYN|THESIS MED CHEM PTY.... (2 Replies)
Discussion started by: BrahmaNaiduA
2 Replies

3. Shell Programming and Scripting

Extract string between two delimiter

I want a string between two delimeter like ( ) from file. Input File, 2007_08_07_IA-0100-014_(January).PDF 2007_08_07_IA-0100-031_(January February March April June July).PDF 2008-02-28_KR-1022-003_(January febuary march april may).CSV Output File, January January February... (19 Replies)
Discussion started by: Pratik Majithia
19 Replies

4. Shell Programming and Scripting

Shell script to put delimiter for a no delimiter variable length text file

Hi, I have a No Delimiter variable length text file with following schema - Column Name Data length Firstname 5 Lastname 5 age 3 phoneno1 10 phoneno2 10 phoneno3 10 sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies

5. Programming

Handle int listen(int sockfd, int backlog) in TCP

Hi, from the manual listen(2): listen for connections on socket - Linux man page It has a parameter called backlog and it limits the maximum length of queue of pending list. If I set backlog to 128, is it means no more than 128 packets can be handled by server? If I have three... (3 Replies)
Discussion started by: sehang
3 Replies

6. Shell Programming and Scripting

Extract semicolon separated delimiter

The log reads as follows. fname1;lname1;eid1;addr;pincode1; fname2;lname2;eid2;addr2;pincode2; fname3;lname3;eid3;addr3;pincode3; fname4;lname4;eid;addr4;pincode4; how do i extract only fname and save it in an array similarly for lname and so on i tried reading a file and cutting each... (5 Replies)
Discussion started by: vkca
5 Replies

7. Shell Programming and Scripting

extract string from varying delimiter line

Hi I have lines like this a=1, b=2, c=3, a=1, d=4, e=5, b=225, I need to extract the b=nnn... value. I dont know how many other entries will be before and after it in each line. Ive tried a basic line like awk '/b=/, $NF ~ /,/ ' myfile.txt but I think that it doesnt care which comma it... (5 Replies)
Discussion started by: rebelbuttmunch
5 Replies

8. UNIX for Dummies Questions & Answers

extract fields from text file using delimiter!!

Hi All, I am new to unix scripting, please help me in solving this assignment.. I have a scenario, as follows: 1. i have a text file(read1.txt) with the following data sairam,123 kamal,122 etc.. 2. I have to write a unix... (6 Replies)
Discussion started by: G.K.K
6 Replies

9. Shell Programming and Scripting

Insert text between delimiter

Can someone help me on this? I'm creating an Insert stmt script but Oracle does not accept blanks values. How can I insert the word null between two commas? I'm guessing awk or sed. Is there a good post or site with easy to understand info on awk and sed? I'm really new to unix scripts :D ... (5 Replies)
Discussion started by: ystee
5 Replies

10. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

hello everybody! I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fileDescriptor; fileDescriptor =... (2 Replies)
Discussion started by: csnmgeek
2 Replies
Login or Register to Ask a Question