![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
This will give you a start:
Code:
echo "Uptime: 816351 Threads: 19 Questions: 8017" | awk '{ print $1 $2 "\n" $3 $4 "\n" $5 $6 }'
|
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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?
|
|
#5
|
|||
|
|||
|
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 |
|
#6
|
||||
|
||||
|
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
$
|
|
#7
|
|||
|
|||
|
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 |
|||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|