It is currently Fri Apr 28, 2017 11:18 pm

[request] help with how to extract number (string) from file

All times are UTC - 6 hours

Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Mon Mar 21, 2011 9:59 am   

Joined: Sat May 15, 2010 10:43 am
Posts: 8
Hi I have a script where I put info from one command into a file with below:

gnokii --monitor once > get_sms_id

what I now want is for the script to search for the string: "callid: " inside this file and output the number which comes after "callid: "

The file contains a line which says:
CALL0: SMS 0644411 () (callid: 4, duration: 0 sec)
So I want to "extract" 4 into a variable but I dont know how to do this.

So the number always come after "id: " and before "," so the number could be 4 or 4976.

Next :), if the "CALL0:" in the file outputs: IDLE like this: CALL0: IDLE I want the script to exit.

Can anyone help me with this? I would be trule greatful! :)

If you need more info, just post here and I'll try to explain better.
Thanks in advance!

 PostPosted: Mon Mar 21, 2011 9:32 pm   

Joined: Mon May 31, 2010 7:40 pm
Posts: 25
Ok, so this may not answer your question in full, but it should atleast point you in the right direction.

Ok so say I have a file called number.txt with the following contents:

[[email protected] bash]$ cat number.txt
1 2 3

Above you see that whilst using the cat command againts the number.txt file has the following numbers separated by spaces.

I want to take the output of the cat command and extract just one of the fields from this file, in this case the number three.

So the step is to pipe (|) the output of cat and feed it to the cut command as follows"

[[email protected] bash]$ cat number.txt|cut -d " " -f 3

So when you pipe the output to cat we use the delimeter field "space" using the -d switch and extract field number three (in this case number three from the file above).

You can follow the same format and try to extract the necessary information you need.

 PostPosted: Tue Mar 22, 2011 1:25 am   

Joined: Sat May 15, 2010 10:43 am
Posts: 8
Hi my textfile outputs this:

LAC: f418 (62488), CellID: 0000ea2c (59948)
RFLevel: 100
Battery: 100
SIM: Used 40, Free 210
Phone: Used 0, Free 500
DC: Used 1, Free 255
EN: Used 0, Free 12544
FD: Used 0, Free 12544
MC: Used 0, Free 512
ON: Used 0, Free 10
RC: Used 0, Free 768
SMS Messages: Unread 0, Number 23
CALL0: SMS 37363892 () (callid: 4, duration: 0 sec)

And I want it to search the entire file for "callid: " and end at "," so anything in between these two: which would output 4. Sorry I dont understand how I can do that with cut?

Thanks for the reply!

 PostPosted: Tue Mar 22, 2011 3:41 am   
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 265
Location: London - UK
tmpvar=$(awk '/callid/ { print $6 }' inputfile)
echo ${tmpvar/,/}

The first line extracts column 6 (separated by spaces) from any line matching /callid/ and stores it, the second line removes the , from the output.
Hopefully this'll help you get to what you need.

 PostPosted: Tue Mar 22, 2011 4:16 am   

Joined: Sat May 15, 2010 10:43 am
Posts: 8
Thanks, I got it working with sed,

sed -n 's/.*callid: \(.*\),.*/\1/p' "file"

Just wanted to fill in this if in the feature someone is searching for a similar question.

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: No registered users and 10 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  

BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group