Trying to use a variable pulled from a for statement in a search string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to use a variable pulled from a for statement in a search string
# 1  
Old 08-30-2010
Trying to use a variable pulled from a for statement in a search string

Guys I am having trouble trying to pull some data from a file using a value returned from a for statement. Any help would greatly be apprecaited. I can't seem to get my script to pass the variable. I am trying to pull the 2 lines below the line that has the "pool name" so that i can get the monitor information.

Raw Data file that I am pulling data from example of file. There is a bunch more (200,000 lines to be exact) data in the file but for example here I pared it down to what I am searching for:
Code:
pool Happy_9120_Pool {
   lb method member observed
   monitor all System_Dead and I_don't_wanna_work_Monitor_9120
   members
      1.2.3.1:9120
      1.2.3.2:9120
      1.2.3.3:9120
      1.2.3.4:9120
}
pool Happy_9121_Pool {
   lb method member observed
   monitor all System_Dead and I_don't_wanna_work_Monitor_9121
   members
      1.2.3.1:9121
      1.2.3.2:9121
      1.2.3.3:9121
      1.2.3.4:9121
}
pool Happy_9122_Pool {
   lb method member observed
   monitor all System_Dead and I_don't_wanna_work_Monitor_9122
   members
      1.2.3.1:9122
      1.2.3.2:9122
      1.2.3.3:9122
      1.2.3.4:9122
}

Extracted Date file that I am searching for and example of file. The pool name is the only thing in this file:
Code:
Happy_9120_Pool
Happy_9121_Pool
Happy_9122_Pool

Expected/Desired Output:
Code:
pool Happy_9120_Pool {
   lb method member observed
   monitor all System_Dead and I_don't_wanna_work_Monitor_9120
pool Happy_9121_Pool {
   lb method member observed
   monitor all System_Dead and I_don't_wanna_work_Monitor_9121
pool Happy_9122_Pool {
   lb method member observed
   monitor all System_Dead and I_don't_wanna_work_Monitor_9122

My Script that I am trying to make work:
Code:
config=/mnt/pool_data_file
config1=/mnt/raw_data_file
tempfile=/mnt/configs_to_delete.txt
for i in `cat $config`
    do
echo $i
awk '/pool $i {/{getline;print;getline;print}' $config1

done

Manual command that works
Code:
awk '/pool Happy_9120_Pool {/{getline;print;getline;print}' /mnt/raw_data_file


Last edited by Franklin52; 08-30-2010 at 05:16 PM.. Reason: Replaced table tags with code tags
# 2  
Old 08-30-2010
Try this command:
Code:
awk 'NR==FNR{a[$0];next}
$2 in a {print;getline;print;getline;print}
' $config $config1

instead of the loop:
Code:
for i in `cat $config`
do
  echo $i
  awk '/pool $i {/{getline;print;getline;print}' $config1
done

# 3  
Old 08-30-2010
When I try that logic it only does the last line of the extracted data file instead of returning results for each of the lines.

Thanks in Advance.
# 4  
Old 08-30-2010
Works fine for me:
Code:
$ cat file1
pool Happy_9120_Pool {
   lb method member observed
   monitor all System_Dead and I_don't_wanna_work_Monitor_9120
   members
      1.2.3.1:9120
      1.2.3.2:9120
      1.2.3.3:9120
      1.2.3.4:9120
}
pool Happy_9121_Pool {
   lb method member observed
   monitor all System_Dead and I_don't_wanna_work_Monitor_9121
   members
      1.2.3.1:9121
      1.2.3.2:9121
      1.2.3.3:9121
      1.2.3.4:9121
}
pool Happy_9122_Pool {
   lb method member observed
   monitor all System_Dead and I_don't_wanna_work_Monitor_9122
   members
      1.2.3.1:9122
      1.2.3.2:9122
      1.2.3.3:9122
      1.2.3.4:9122
$
$ cat file2
Happy_9120_Pool
Happy_9121_Pool
Happy_9122_Pool
$
$ awk 'NR==FNR{a[$0];next}
$2 in a {print;getline;print;getline;print}
' file2 file1
pool Happy_9120_Pool {
   lb method member observed
   monitor all System_Dead and I_don't_wanna_work_Monitor_9120
pool Happy_9121_Pool {
   lb method member observed
   monitor all System_Dead and I_don't_wanna_work_Monitor_9121
pool Happy_9122_Pool {
   lb method member observed
   monitor all System_Dead and I_don't_wanna_work_Monitor_9122

# 5  
Old 08-30-2010
It might have to do with the flavor of O/S (Solaris 10) and the version of awk I have. I get the results when I use nawk that is loaded on the box.
# 6  
Old 08-30-2010
grep alternative

Code:
~/$ grep -A2 -f file2 file1
pool Happy_9120_Pool {
   lb method member observed
   monitor all System_Dead and I_don't_wanna_work_Monitor_9120
--
pool Happy_9121_Pool {
   lb method member observed
   monitor all System_Dead and I_don't_wanna_work_Monitor_9121
--
pool Happy_9122_Pool {
   lb method member observed
   monitor all System_Dead and I_don't_wanna_work_Monitor_9122

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help with how to search a file for a variable string and delete that line

Hi, I have a working script. It does what I am intending it to but a bit confused whether the sed part is supposed to be working or not. Further down is the script with the sed part that should have been working but not and the grep -v part which is the workaround that I am using at the... (10 Replies)
Discussion started by: newbie_01
10 Replies

2. Shell Programming and Scripting

Search for string in column using variable: awk

I'm interested to match column pattern through awk using an external variable for data: -9 1:751343:T:A -9 0 T A 0.726 -5.408837e-03 9.576603e-03 7.967536e-01 5.722312e-01 -9 1:751756:T:C -9 0 T C 0.727 -5.360458e-03 9.579447e-03 7.966977e-01 5.757858e-01... (7 Replies)
Discussion started by: genome
7 Replies

3. UNIX for Advanced & Expert Users

Pass variable to awk command search string

I must have forgot how to do this, but, I am attempting to enter a variable into an awk / gawk search pattern. I am getting a value from user input to place in a specific section of a 132 character string. my default command is .... gawk --re-interval '/^(.{3}P .{4}CYA.{8}1)/' ... (3 Replies)
Discussion started by: sdeevers
3 Replies

4. Shell Programming and Scripting

Search for string in a file, extract two another strings and concatenate to a variable

I have a file with <suit:run date="Trump Tue 06/19/2012 11:41 AM EDT" machine="garg-ln" build="19921" level="beta" release="6.1.5" os="Linux"> Need to find word "build" then extract build number, which is 19921 also release number, which is 6.1.5 then concatenate them to one variable as... (6 Replies)
Discussion started by: garg
6 Replies

5. Shell Programming and Scripting

Search file for string and store last result to variable

Hi, I'm trying to search a text file for a string: "energy(sigma->0)=". To do so, I executed the grep command, which returned many matches, for example: energy without entropy = -112.16486170 energy(sigma->0) = -112.16520778 energy without entropy = -112.16488936 ... (5 Replies)
Discussion started by: gwr
5 Replies

6. Shell Programming and Scripting

sed doubt - search and substitute string from variable.

hi, trying to learn more abt sed :( i want to substitute a variable(a) with other variable(b) appended. Read.txt contains: home/test2/abc home/test/root1 input.txt contains: make test "home/test1/none"version="1.3" wt's wrong test "home/test2/abc"version="1.0" make save... (9 Replies)
Discussion started by: dragon.1431
9 Replies

7. Shell Programming and Scripting

Shell Script to Search for a particular String and copy the timestamp to a variable

Hi, We Perfrom Loads to the database through a Perl script which generates a statistics file. I need to read the statistics. the Statistics file looks something like below: Process Beginning - 08-26-2010-23.41.47 DB2 CONNECTION SUCCESSFUL! Ready to process and load file: FILENAME # of... (2 Replies)
Discussion started by: Praveenkulkarni
2 Replies

8. Shell Programming and Scripting

How to assign the Pattern Search string as Input Variable

guys, I need to know how to assing pattern matched string as an input command variable. Here it goes' My script is something like this. ./routing.sh <Server> <enable|disable> ## This Script takes an input <Server> variable from this line of the script ## echo $1 | egrep... (1 Reply)
Discussion started by: raghunsi
1 Replies

9. Shell Programming and Scripting

search a string in a line and save it in a variable

Hi I want to read a file line by line and search for a particular string in each line(say for example string containing @ )and save that string into a variable. Can someone suggest me the way to implement it.I am using K- shell Thanks Ishita (5 Replies)
Discussion started by: Ishita
5 Replies

10. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies
Login or Register to Ask a Question