Extracting data from a shell output?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting data from a shell output?
# 8  
Old 02-02-2009
Code:
fetch_indexinfo()
{
  eval "$( $indexer "$1" |
      awk '/^total.*docs/ {printf "docs=%d size=%d", $2, $4; exit}')"
}

# 9  
Old 02-02-2009
Thanks a lot, works perfectly!
# 10  
Old 02-02-2009
Quote:
Originally Posted by cfajohnson

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" )"

Another lesson learned! Thanks! Smilie
# 11  
Old 02-05-2009
Related to the same shell output:
Code:
indexing index 'delta'...
ERROR, (error message here)...
WARNING, (warning message here)...
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

Sometimes, there is an error or warning thrown out by the program.
I would like to use the same type of function based on awk(), that looks into text output for the ERROR and WARNING words and sets a flag.

For example:
Code:
function error_found()
{
	flag=0
	eval $string
	if [ word ERROR or WARNING found ]; then
		flag=1
	fi
}

Thanks for your help.
# 12  
Old 02-05-2009
Quote:
Originally Posted by TECK
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...

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

Quote:
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).

Code:
indexer="/usr/bin/indexer"
fetch_indexinfo()
{
  eval "$(
     $indexer $1 |
     awk '/^total.*docs/ {printf "docs=%d size=%d", $2, $4; exit}'
   )"
}


Last edited by cfajohnson; 02-06-2009 at 12:59 PM..
# 13  
Old 02-05-2009
Chris, do you recommend me to use the same approach? (looking at your quoted code)
I think my question was not clear, sorry about that.
Basically, I need to search into this string with awk for either ERROR or WARNING:
Code:
indexing index 'delta'...
ERROR, (error message here)...
WARNING, (warning message here)...
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

In the above example, both words were found. I have no idea what would be the awk format to return a flag, if any of the ERROR or WARNING words are found in string.

Code:
indexer="/usr/bin/indexer"
function fetch_indexinfo()
{
  eval "$(
     $indexer $1 |
     awk '/ERROR or WARNING found in string?/ {return flag=1 else return flag=0}'
   )"
}

Thanks for your help.

Last edited by TECK; 02-05-2009 at 03:57 AM..
# 14  
Old 02-13-2009
Just a friendly reminder that I did not solved yet my issue and still looking for help.
Running $indexer will output this text:
Code:
indexing index 'delta'...
ERROR: this is an error message.
WARNING: this is a warning message.
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 can I incorporate into same function Chris made 2 more variables (error and warning)?

I tried this:
Code:
fetch_indexinfo()
{
    eval "$(
        $indexer "$1" |
        awk '/^total.*docs/ {printf "docs=%d size=%d", $2, $4; exit}' |
        awk '/^ERROR:/ {printf "error=%d", $1; exit}' |
        awk '/^WARNING:/ {printf "warning=%d", $1; exit}'
    )"
}

If I use this function neither of the defined variables will work.
Thank you for helping me solve the problem.

Floren
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