Extract number that comes after a given string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract number that comes after a given string
# 1  
Old 11-23-2010
Extract number that comes after a given string

Hi!

I have a file that contains non-regular strings like:
Code:
SCSGTI:N="$4,0,1,4,34622991111-->RemoteSPC: 1111", NWID=1;
SCSGTI:N="$4,0,1,4,34622991211-->RemoteSPC: 1211", NWID=1;
SCSGTI:N="$4,0,1,4,*-->RemoteSPC: 2112,Sec:RemoteSPC: 2212", NWID=1;
SCSGTI:N="$4,10,1,4,34622999213-->RemoteSPC: 9211 SSN: 1,Sec:RemoteSPC: 9212 SSN: 11", NWID=1;

I need to loop the file and extract some numbers and put them into variables as follows:
Code:
GT=34622991111 SPC1=1111
GT=34622991211 SPC1=1211
GT=* SPC1=2112 SPC2=2212
GT=34622999213 SPC1=9211 SSN1=11 SPC2=9212 SSN1=11

The problems is the only fixed fields are GT and SPC1. SPC2, SSN1 and SSN2 are optional. So in words the criteria for extracting these fields would be:
Code:
GT = string between "," and "-->"
SPC1 = first number after "-->RemoteSPC:"
SPC2 = first number after "Sec:RemoteSPC:"
SSN1 = first number after 1st occurance of "SSN:"
SSN2 = first number after 2nd occurance of "SSN:"

Can anyone help me translate this into bash commands?

Thanks

Last edited by Franklin52; 11-23-2010 at 11:58 AM.. Reason: Please use code tags
# 2  
Old 11-23-2010
Do you mean like this?
Code:
$ sed 's/SCSGTI:N="$\([0-9]*,\)\{4\}/GT=/;s/-->RemoteSPC: / SPC1=/;s/SSN: /SSN1=/;s/,Sec:RemoteSPC: / SPC2=/;s/SSN: /SSN2=/;s/",.*//' infile
GT=34622991111 SPC1=1111
GT=34622991211 SPC1=1211
GT=* SPC1=2112 SPC2=2212
GT=34622999213 SPC1=9211 SSN1=1 SPC2=9212 SSN2=11

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 11-23-2010
This is pretty close. What I want actually is to store each of those numbers in variables within a script, so I can use them later in the code:
Code:
 
for i in `cat infile.txt`
do
 
GT=`echo $i | sed 's/SCSGTI:N="$\([0-9]*,\)\{4\}/GT=/;s/-->RemoteSPC: / /'`
PC1=`echo $i | sed...`
SSN1=`echo $i | sed...`
PC2=`echo $i | sed...`
SSN2=`echo $i | sed...'`
 
echo $GT
echi $PC1
...
 
done

I tried to chop down your one liner so I can get only the GT and so on, but couldn't get it right:
Code:
sed 's/SCSGTI:N="$\([0-9]*,\)\{4\}//;s/-->RemoteSPC: / /' infile
34622991111 1111", NWID=1;
34622991211 1211", NWID=1;
34622999213 9211 SSN: 11,Sec:RemoteSPC: 9212 SSN: 11", NWID=1;
* 2112,Sec:RemoteSPC: 2212", NWID=1;

Moderator's Comments:
Mod Comment Please use code tags

Last edited by Scott; 11-23-2010 at 02:43 PM..
# 4  
Old 11-23-2010
This might work:
Code:
sed 's/SCSGTI:N="$\([0-9]*,\)\{4\}\(.*\)-->RemoteSPC: \([^," ]*\)\( SSN: \([^," ]*\)\)*\(,Sec:RemoteSPC: \([^," ]*\)\)*\( SSN: \([^," ]*\)\)*",.*/\2,\3,\5,\7,\9/' infile | 
while IFS=, read GT SPC1 SSN1 SPC2 SSN2
do
  echo "$GT"
  echo "$SPC1"
  echo "$SSN2"
done

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 11-23-2010
I think I'll go with your one liner and work on its printout. I then can easily extract all the fields I need, by using separators GT, SPC, etc on the left and space on the right.

To make it more general I could write a routine which takes a field name as input (e.g. RemoteSPC), use it as a separator and then check each char that comes after to see if it's a number. Stop when a non-number is reached. Not very Unixy, more like Pascalish.

Thanks a lot.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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 (14 Replies)
Discussion started by: dsravanam
14 Replies

2. Shell Programming and Scripting

How to extract every repeated string between two specific string?

Hello guys, I have problem with hpux shell script. I have one big text file that contains like SOH bla bla bla bla bla bla ETX SOH bla bla bla ETX SOH bla bla bla ETX What I need to do is save first SOH*BLA into file1.txt, save second SOH*BLA into file2.txt and so on.... (17 Replies)
Discussion started by: sembii
17 Replies

3. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

4. Shell Programming and Scripting

Extract number part from the string in ksh 88

I have to extract number part (Date and timestamp part ) from the following 3 strings AB_XYZA_20130930183017.log AB_DY_XYZA_20130930183017.log AB_GZU_20130930183017.log Output should be 20130930183017 Please help me to get the string like above Thanks (2 Replies)
Discussion started by: smile689
2 Replies

5. Shell Programming and Scripting

how to extract last word and a number from a string

I have the following script (which I made by my self) #!/bin/bash # add a few empty lines to make it more legible # add a date description on each update interval echo "" >> /home/user/DYN_DNS_IP_change.log echo "" >> /home/user/DYN_DNS_IP_change.log echo "" >>... (6 Replies)
Discussion started by: mahirzukic2
6 Replies

6. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

7. Shell Programming and Scripting

Extract string from multiple file based on line count number

Hi, I search all forum, but I can not find solutions of my problem :( I have multiple files (5000 files), inside there is this data : FILE 1: 1195.921 -898.995 0.750312E-02-0.497526E-02 0.195382E-05 0.609417E-05 -2021.287 1305.479-0.819754E-02 0.107572E-01 0.313018E-05 0.885066E-05 ... (15 Replies)
Discussion started by: guns
15 Replies

8. Shell Programming and Scripting

changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as name1:name2:name3:1 when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
Discussion started by: Learnerabc
3 Replies

9. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

10. Shell Programming and Scripting

How to extract variable number from a String

hi,I am new to shell script,I have String,like this: Number of rows exported: 5321 the numbe at end could changing,how can I extract this number and assign it to a variable,then use it later in script. thanks. (19 Replies)
Discussion started by: vitesse
19 Replies
Login or Register to Ask a Question