Pull specific data from repetitive text?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pull specific data from repetitive text?
# 1  
Old 02-19-2010
Pull specific data from repetitive text?

Hi all,

I have a bit of a problem with a loop that extracts repetitive text.

Running /bin/index executable outputs this text to terminal:
Code:
testing index 'scanthread'...
collected 0 docs, 0.0 MB
total 0 docs, 0 bytes
total 0.012 sec, 0 bytes/sec, 0.00 docs/sec
testing index 'scanpost'...
collected 1 docs, 0.0 MB
sorted 0.0 Mhits, 97.6% done
total 1 docs, 28 bytes
total 0.016 sec, 1725 bytes/sec, 61.62 docs/sec
testing index 'scankeyword'...
collected 0 docs, 0.0 MB
total 0 docs, 0 bytes
total 0.002 sec, 0 bytes/sec, 0.00 docs/sec
total 14 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg

My goal is to eval the command and pull the blue values from text, in a loop, and print them as a set:
Code:
test_index()
{
	indexlist=$(
		$indexer --config $CONF_PATH --run $1 |
		awk '/^total.*docs.*bytes$/ {printf "records=%d size=%d\n", $2, $4}'
	)
	for index in $indexlist; do
		echo "$index $records $size"
	done
}

index[0]="scanthread"
index[1]="scanpost"
index[2]="scankeyword"
test_index "${index[*]}"

The first issue I have is: I don't know how to filter the index title with awk (i.e. 'scanthread', with or without quotes, it does not matters).
Second, if I print the $records in the loop, it will print 6 times (the wrong value), instead of 3?

Thanks for your help.
# 2  
Old 02-19-2010

Does this do what you want?

Code:
awk '/scan/ { print; x = 1 }
(x == 1) && /total/ { print; x = 0 }'

# 3  
Old 02-19-2010
Hi Chris,

No, it does not. The index names are just an example, they could be anything:
index[0]="testalpha"
index[1]="johndoe"
index[2]="dancedance"

Just to clarify better what I try to achieve:

I have a script that generates an array that contains index names, for example:
index[0]="testalpha"
index[1]="johndoe"
index[2]="dancedance"


Then, the script executes the command ($1 is actually ${index[*]}):
$indexer --config $CONF_PATH --run $1

The above line will output the following test to terminal:
Code:
testing index 'testalpha'...
collected 0 docs, 0.0 MB
total 0 docs, 0 bytes
total 0.012 sec, 0 bytes/sec, 0.00 docs/sec
testing index 'johndoe'...
collected 1 docs, 0.0 MB
sorted 0.0 Mhits, 97.6% done
total 1 docs, 28 bytes
total 0.016 sec, 1725 bytes/sec, 61.62 docs/sec
testing index 'dancedance'...
collected 0 docs, 0.0 MB
total 0 docs, 0 bytes
total 0.002 sec, 0 bytes/sec, 0.00 docs/sec
total 14 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg

As you can see, each index is associated with this text format:
testing index 'johndoe'...
collected 1 docs, 0.0 MB
... more lines here ...
total 1 docs, 28 bytes
total 0.016 sec, 1725 bytes/sec, 61.62 docs/sec


My goal is to create a loop that will associate the blue values to variables.
Line: testing index 'johndoe'...
should grab the 'johndoe' text and assign it to $index variable.
Line: total 1 docs, 28 bytes
should grab the 1 and 28 values and assign them to $records and $size variables.
* the format of those lines never changes, so we can use the words as delimiters.

Then, still in the loop, I can simply print the variables or use them the way I like.

Code prototype:
Code:
	indexlist=$(
		$indexer --config $CONF_PATH --run $1 |
		awk '/^testing index.*/ {printf "index=%d\n", $1}' |
		awk '/^total.*docs.*bytes$/ {printf "records=%d size=%d\n", $2, $4}'
	)
	for i in $indexlist; do
		echo $index
		echo $records
		echo $size
	done

The output should be:
'testalpha' 0 0
'johndoe' 1 28
'dancedance' 0 0

Last edited by TECK; 02-19-2010 at 01:48 PM..
# 4  
Old 02-19-2010
Try this:

Code:
awk '/testing/{idx=$3}
/^total/ && idx {print idx, $2, $4; idx=""}'

# 5  
Old 02-19-2010
I need to define variables with awk: $index, $records, $size
See my non-working code prototype, used as example:
Code:
	indexlist=$(
		$indexer --config $CONF_PATH --run $1 |
		awk '/^testing index.*/ {printf "index=%d\n", $1}' |
		awk '/^total.*docs.*bytes$/ {printf "records=%d size=%d\n", $2, $4}'
	)
	for i in $indexlist; do
		echo $index
		echo $records
		echo $size
	done

# 6  
Old 02-19-2010
Have you tried the command with the output of your executable?

Code:
/bin/index | awk '/testing/{idx=$3}
/^total/ && idx {print idx, $2, $4; idx=""}'

# 7  
Old 02-19-2010
Yes, I did. It does not return anything.
I did fixed the issue, thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read all data after a specific string from a text file ?

Hi, I have a file(input.txt) and trying to format as output.txt. See the attached file format. Note: This is a windows file (DOS format) and the commands are also going to execute on windows. Basically I am trying to capture all the data in between Local Group Memberships and Global Group... (10 Replies)
Discussion started by: Monoj2014
10 Replies

2. Shell Programming and Scripting

[Solved] How can I pull specific information from PS?

I need to grab information from the output of the ps command. For each line of ps output that contains _progres -b I need to get the word that follows -p. The "-p" can be anywhere after "_progres -b". Using grep to select the correct lines is no problem (e.g. ps -ef|grep "_progres \-b|grep -v... (3 Replies)
Discussion started by: Papa Lee
3 Replies

3. Shell Programming and Scripting

Converting text files to xls through awk script for specific data format

Dear Friends, I am in urgent need for awk/sed/sh script for converting a specific data format (.txt) to .xls. The input is as follows: >gi|1234|ref| Query = 1 - 65, Target = 1677 - 1733 Score = 8.38, E = 0.6529, P = 0.0001513, GC = 46 fd sdfsdfsdfsdf fsdfdsfdfdfdfdfdf... (6 Replies)
Discussion started by: Amit1
6 Replies

4. Shell Programming and Scripting

pull out rows with specific criteria

Hi, I have a file with multiple columns and for column 5 I want to extract that row into another file if it is equal to or greater than a certain value. For example: FAR 4 5 7 LOP GAT 3 3 3 POL I want values that are greater than 5 for column 4. So the final file will look like... (1 Reply)
Discussion started by: phil_heath
1 Replies

5. Shell Programming and Scripting

Pull specific lines from long file based on formula

For people who want to pull a number of lines from a long file using a specific formula n (number of iterations in a loop) a (offset number) b (stretch factor) example with n {1..100} for (( n=1; n<101; n++ )); do awk -v n=$n 'NR==a+(b*n)' a=0 b=1 inputfile >>outputfile (2 Replies)
Discussion started by: sgruenwald
2 Replies

6. Shell Programming and Scripting

Need help in writing a script to create a new text file with specific data from existing two files

Hi, I have two text files. Need to create a third text file extracting specific data from first two existing files.. Text File 1: Format contains: SQL*Loader: Release 10.2.0.1.0 - Production on Wed Aug 4 21:06:34 2010 some text ............so on...and somwhere text like: Record 1:... (1 Reply)
Discussion started by: shashi143ibm
1 Replies

7. Shell Programming and Scripting

How pull the Data from the file and should be mailed

Hi All, I need to pull the data from a file from 2 set of directories & should be mailed. I have a code , it is failing for some reason. Do somebody correct me where my script is going wrong. Directories: 1st Set of Directory /176_PS/Transactions/**/syslog.log ** : prd1 prd2... (2 Replies)
Discussion started by: raghunsi
2 Replies

8. Shell Programming and Scripting

How do you use pull data from multiple lines to do a for statement?

Guys I am having a problem with being able to find missing monitors in a configuration check script I am trying to create for accountability purposes for managing a large number of systems. What I am trying to do is run a script that will look at the raw config data in a file and pull all the pool... (7 Replies)
Discussion started by: scottzx7rr
7 Replies

9. Shell Programming and Scripting

Reading data from a specific line in a text file

Hello, I have a problem which is giving me headache for days, can some please help. Please see code and text fiel below. Please see text in red for the problem I am facing # Program gets an input x from user while read line ; do echo... (4 Replies)
Discussion started by: jermaine4ever
4 Replies

10. Shell Programming and Scripting

Reading data from a specific line in a text file

hello, I have got the following problem that I am hoping someone can help with please. 1. I have got the following text file (below) , the columns data are 'Test Day', 'Board', 'Betting Number'. TEXT FILE ============================================ 1 3 02-01-27-28-29-30 0 1... (1 Reply)
Discussion started by: jermaine4ever
1 Replies
Login or Register to Ask a Question