Extracting data from a shell output?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting data from a shell output?
# 1  
Old 02-01-2009
Extracting data from a shell output?

Hi all,

I use a sh script to run few commands on CentOS 5.2. My goal is to search into test output for specific content and save it into a database table. So far everything is simple, except that I do not know how to grab the specific values:

Code:
#!/bin/sh

# Source function library.
. /etc/rc.d/init.d/functions

prog="searchd"
indexer="/usr/bin/indexer"
mysql="/usr/bin/mysql --defaults-file=myadmin.cnf"
database="test"

if [ $(pidofproc $prog) ]; then
$indexer delta
$mysql <<SQL
    USE $database;
    UPDATE table SET
        docs = '$docs',
        size = $size;
    QUIT
SQL
fi

Now, when I run the indexer command, the output text is in this format:
Code:
indexing index 'delta'...
collected 1953887 docs, 821.3 MB
sorted 129.7 Mhits, 100.0% done
total 1953887 docs, 821304246 bytes
total 687.916 sec, 1193902.12 bytes/sec, 2840.30 docs/sec
total 278 reads, 18.6 sec, 3891.4 kb/read avg, 66.9 msec/read avg
total 3770 writes, 9.9 sec, 567.3 kb/write avg, 2.6 msec/write avg

How do I grab the above highlighted values from outputted text and define them as variables?
docs=1953887
size=821304246

Thanks a lot for your help.

Floren
# 2  
Old 02-01-2009
I'm not sure if this is what you need:

Code:
x=`cat foo | grep "^total [0-9]* docs" | awk '{print $2}'`
y=`cat foo | grep "^total [0-9]* docs" | awk '{print $4}'`

# 3  
Old 02-02-2009
Quote:
Originally Posted by angheloko
I'm not sure if this is what you need:

Code:
x=`cat foo | grep "^total [0-9]* docs" | awk '{print $2}'`
y=`cat foo | grep "^total [0-9]* docs" | awk '{print $4}'`


He certainly doesn't need two cats, two greps and two awks.

Code:
eval "$( awk '/^total.*docs/ { printf "docs=%d size=%d", $2, $4 ; exit}' "$FILE" )"

# 4  
Old 02-02-2009
Code:
read docs size <  <(awk '/^total [0-9]+ docs, [0-9]+ bytes/ { print $0=$2 FS $4 }' file)

# 5  
Old 02-02-2009
Quote:
Originally Posted by rubin
Code:
read docs size <  <(awk '/^total [0-9]+ docs, [0-9]+ bytes/ { print $0=$2 FS $4 }' file)

Code:
$ read docs size <  <(awk '/^total [0-9]+ docs, [0-9]+ bytes/ { print $0=$2 FS $4 }' file)
Syntax error: redirection unexpected

# 6  
Old 02-02-2009
Quote:
Originally Posted by cfajohnson
Code:
$ read docs size <  <(awk '/^total [0-9]+ docs, [0-9]+ bytes/ { print $0=$2 FS $4 }' file)
Syntax error: redirection unexpected

Assuming bash ( OP is using CentOS 5.2 ).
# 7  
Old 02-02-2009
Thanks a lot guys!
This is what I have so far:
Code:
result=$($indexer delta | awk '/^total.*docs/ {printf "docs=%d size=%d", $2, $4; exit}')
eval $result
echo $docs

Output:
1953887

Is there a more compact way to write the $result line or to incorporate the eval into it? I'm new at shell scripting, thank you for understanding...

Ideally, I would like to have a function that does the job, because I want to use it for different indexes (ie. alpha, beta, delta, etc.):
Code:
indexer="/usr/bin/indexer"
function fetch_indexinfo()
{
	result=$($indexer $0 | awk '/^total.*docs/ {printf "docs=%d size=%d", $2, $4; exit}')
	eval $result
}

Not sure how write the function, honestly. I plan to use it this way:
Code:
fetch_indexinfo delta
echo $docs
echo $size

The function will execute the command, then grab for the outputted text the values I'm interested (like it does it now with the direct command).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run sql query in shell script and output data save as delimited text

I want to run sql query in shell script and output data save as delimited text (delimited text would be comma) Code: SPOOL_FILE=/pgedw/dan.txt SQL=/pgedw/dan.sql sqlplus -s username/password@myhost:port/servicename <<EOF set head on set COLSEP , set linesize 32767 SET TRIMSPOOL ON SET... (8 Replies)
Discussion started by: Jaganjag
8 Replies

2. Shell Programming and Scripting

Help with extracting data within parentheses

This is my input file: a|b|c(ef)|g|h(km)|p My output file should look like: a|b|ef|g|km|p That is, pipe is the delimiter. The data within pipe must be displayed as it is but if it encounters any data within parentheses, then only the data within parentheses has to be displayed ( the data... (2 Replies)
Discussion started by: ksatish89
2 Replies

3. Shell Programming and Scripting

Extracting data from file-shell scripting--please help

hello friends, my file is like 123 |asd|asd|asd 123_1|awd|asw|asw 121 |wer|qwe|wee 124 |weq|qwe|iop 1_23 |bla|blh|bha 145 |ghj|jkl|ghj 146 |qwe|qwe|wer 154 |asd|wer|qw_e 134_5|qwe|wer|qw_e is their any solution to retrive only those lines which are having only 3 numerical letters... (20 Replies)
Discussion started by: PankajChawla
20 Replies

4. Shell Programming and Scripting

extracting data

I have a txt file of the following format >ab_ qwerty >rt_ hfjkil >Ty2 hglashglkasghkf; >P2 aklhfklflkkgfgkfl >ui_ vnllkdskkkffkfkkf >we32 vksksjksj;lslsf'sk's's .... ..... I want to split this big file based on the header (>) (5 Replies)
Discussion started by: Lucky Ali
5 Replies

5. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

6. Shell Programming and Scripting

extracting data from a string

Hi there, I have a bunch of vlan tagged network interfaces that are named as follows e1000g111000 e1000g99001 e1000g3456000 nge2002 where the 'e1000g' and 'nge' parts of the name are the driver, the red and blue bits above define the VLAN and the last digit on the end defines the... (3 Replies)
Discussion started by: rethink
3 Replies

7. Shell Programming and Scripting

Extracting data from https server with the help of unix shell script

There is a folder which can be accessed through URL by giving a particular Username and Password.Inside the folder there are few excel sheets.The excel sheets/folder need to be imported from there to unix box with the help of unix shell script. Can anyone help me?Does anyone have code for it?... (2 Replies)
Discussion started by: vanur
2 Replies

8. UNIX for Dummies Questions & Answers

Help with extracting data and plotting

I have attached a txt file, what I would like to be able to do is: 1. Extract Data from Columns labeled E/N and Ko into a new file 2. Then in the new file I would like to be able to plot E/N on the X axis and Ko on the y axis. 3. Lastly I would like to be able to extract multiple data sets and... (6 Replies)
Discussion started by: gingburg
6 Replies

9. Shell Programming and Scripting

urgent-extracting block data from flat file using shell script

Hi, I want to extract block of data from flat file. the data will be like this start of log One two three end of log i want all data between start of log to end of log i.e One two three to be copied to another file. This particular block may appear multiple times in same file. I... (4 Replies)
Discussion started by: shirish_cd
4 Replies

10. Shell Programming and Scripting

how to assign sql output data to shell script variable

Hi Guys ! I am new to unix and want to find out how we can make sql statement data to shell script variable? Any help/suggestion is greatly appreciated -Chandra (1 Reply)
Discussion started by: kattics
1 Replies
Login or Register to Ask a Question