The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
parsing a string satish@123 Shell Programming and Scripting 4 05-16-2008 04:20 AM
parsing string in c phani_sree High Level Programming 3 10-23-2007 11:54 PM
parsing url string vanitham Shell Programming and Scripting 5 10-22-2007 02:48 AM
Parsing string rolex.mp UNIX for Dummies Questions & Answers 3 02-20-2007 10:28 AM
String Parsing help jasjot31 UNIX for Dummies Questions & Answers 5 09-27-2006 02:38 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-13-2006
Registered User
 

Join Date: Sep 2006
Posts: 6
Need help parsing a string

Hi,
I'm writing a shell script that outputs, among other things, some of the information that is outputted by the mysqladmin status command.

The output of the command looks like this:
Uptime: 816351 Threads: 19 Questions: 80719739 Slow queries: 1419 Opens: 15903523 Flush tables: 1 Open tables: 4948 Queries per second avg: 98.879

I need to output the Queries per second, Open tables, and Threads. I know I can count the words on the line and do a cut, but I'd rather do it in a neater fashion.

Is there an easy way to parse this line so that it ouputs:
Uptime: xxxx
Threads: xxx
etc.

and once I have it in that format, I can parse to get the values I want.

Thanks.
Reply With Quote
Forum Sponsor
  #2  
Old 09-13-2006
tayyabq8's Avatar
Moderator
 

Join Date: Nov 2004
Location: Bahrain
Posts: 555
This will give you a start:
Code:
echo "Uptime: 816351 Threads: 19 Questions: 8017" | awk '{ print $1 $2 "\n" $3 $4 "\n" $5 $6 }'
Reply With Quote
  #3  
Old 09-13-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
echo "Uptime: 816351 Threads: 19 Questions: 8017" | read a b c d e f
echo $a $b
echo $c $d
echo $e $f

Last edited by anbu23; 09-13-2006 at 08:33 AM.
Reply With Quote
  #4  
Old 09-13-2006
Registered User
 

Join Date: Sep 2006
Posts: 6
thanks. The problem is that as you get to the later words, some of the "titles" have more than one word such as "open tables". How can I parse that?
Reply With Quote
  #5  
Old 09-13-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
echo "Slow queries: 1419 Opens: 15903523 Flush tables: 1" | 
read a b c d e f g h
echo $a $b $c
echo $d $e
echo $f $g $h
Reply With Quote
  #6  
Old 09-13-2006
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
Try...
Code:
$ cat file
Uptime: 816351 Threads: 19 Questions: 80719739 Slow queries: 1419 Opens: 15903523 Flush tables: 1 Open tables: 4948 Queries per second avg: 98.879
$ awk '{for(i=1;i<=NF;i++)printf $i ($(i-1)~/:$/?ORS:OFS) }' file
Uptime: 816351
Threads: 19
Questions: 80719739
Slow queries: 1419
Opens: 15903523
Flush tables: 1
Open tables: 4948
Queries per second avg: 98.879
$
Reply With Quote
  #7  
Old 09-13-2006
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Alternative in Python:

Code:
>>> import re
>>> s = 'Uptime: 816351 Threads: 19 Questions: 80719739 Slow queries: 1419 Opens: 15903523 Flush tables: 1 Open tables: 4948 Queries per second avg: 98.879'
>>> all = re.findall(r"([a-z A-Z]+): ([0-9.0-9]+)",s)
>>> for i , j in all:
>>>         print i , ":" , j
Uptime : 816351
 Threads : 19
 Questions : 80719739
 Slow queries : 1419
 Opens : 15903523
 Flush tables : 1
 Open tables : 4948
 Queries per second avg : 98.879
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:18 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0