Help with AWK looping


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with AWK looping
# 1  
Old 01-30-2010
Help with AWK looping

I'm trying to parse a configuration text file using awk. The following is a sample from the file I'm searching. I can retrieve the formula and recipe names easily but now I want to take it one step farther. In addition to the formula name, I would like to also get the value of the attribute "ABCD-ZK-O" which is associated with "A-INSTANCE-1". I've read a few books and I can't figure out how to begin looping after I find the formula name. My goal would be to create output that looks like "BF-123456 ABCD-ZK-0". Thank you for any guidance you can offer.

Code:
BATCH_RECIPE_FORMULA NAME="BF-123456" RECIPE="RECIPE-12345"
 user="JDOE" time=1144173097/* "04-Apr-2006 13:51:37" */
{
  VERSION=""
  HIDDEN=F
  RELEASED_TO_PRODUCTION=T
  ATTRIBUTE_INSTANCE NAME="A-INSTANCE-1"
  {
    VALUE { CV="ABCD-ZK-0" }
    VALUE_CHANGED=T
    HAS_DEFAULT_VALUE=F
  }
  ATTRIBUTE_INSTANCE NAME="ANOTHER_ATTRIBUTE"
  {
    VALUE { CV="SOMETHING_ELSE" }
    VALUE_CHANGED=T
    HAS_DEFAULT_VALUE=F
  }
}


Last edited by pludi; 01-30-2010 at 03:50 PM.. Reason: code tags, please...
# 2  
Old 01-30-2010
Not sure what you want but try this
Code:
:~$ awk -F \" '/BATCH_RECIPE_FORMULA NAME/ {printf $2" "} /CV/{print $2}' awktest | head -1
BF-123456 ABCD-ZK-0

# 3  
Old 01-30-2010
Quote:
Originally Posted by krabu
Not sure what you want but try this
Code:
:~$ awk -F \" '/BATCH_RECIPE_FORMULA NAME/ {printf $2" "} /CV/{print $2}' awktest | head -1
BF-123456 ABCD-ZK-0

I think this code will work if he has only one record!!!

try the next in case the record separator is empty line:

Code:
gawk " BEGIN { RS=\"\"; FS=\"\n\" } {split($1,OUT1,\"\" \" \") ; split($9,OUT3,\"\" \" \") ;print OUT1[2],OUT3[3]}" datafile_newline_RS.txt

NAME="BF-123456" CV="ABCD-ZK-0"
NAME="BF-555555" CV="ABCD-ZK-1"
NAME="BF-44444" CV="ABCD-ZK-2"
NAME="BF-123456" CV="ABCD-ZK-3"

in cae no empty lines between recordes try the next
Code:
gawk " BEGIN { RS=\"}\nB\"; FS=\"\n\" } {split($1,OUT1,\"\" \" \") ; split($9,OUT3,\"\" \" \") ;print OUT1[2],OUT3[3]}"  data_file_no_newline_RS.txt

NAME="BF-123456" CV="ABCD-ZK-0"
NAME="BF-555555" CV="ABCD-ZK-1"
NAME="BF-44444" CV="ABCD-ZK-2"
NAME="BF-123456" CV="ABCD-ZK-3"

# 4  
Old 01-31-2010
Or (modifying Krabu's solution):
Code:
awk -F \" '/BATCH_RECIPE_FORMULA NAME/{s=$2} /CV/&&s{print s" "$2;s=""}' infile

# 5  
Old 01-31-2010
Thank you all so much for your help.

Scrutinizers code works returning what I need and more. I realized that some of the records don't have the sub-record I'm looking for so there's a mix of data that's not needed. Can anyone offer a suggestion on how to take this one step further and only report the records that have a matching sub-record equal to "A-INSTANCE-1" that I'm looking for? In other words, only print the formula name and the "A-INSTANCE-1" value when the sub-records contains "A-INSTANCE-1".

Again, thank you all... you are very smart people.

Code:
/BATCH_RECIPE_FORMULA NAME/{s=$2} /CV/&&s{print s" "$2;s=""}

# 6  
Old 01-31-2010
Hi new2awk, try this:
Code:
awk -F \" '/BATCH_RECIPE_FORMULA NAME/{s=$2} /A-INSTANCE-1/{p=1} /CV/&&p{print s" "$2;s="";p=0}' infile

# 7  
Old 02-06-2010
Thank you all for your help. Here's what I ended up with. It took me a bit to realize how awk was processing the file one line at a time and by loading it all first to parse it. Once I figured that out, the light bulb went on.

I found this page very helpful: Common threads: Awk by example, Part 1

Thanks again.

Code:
/BATCH_RECIPE_FORMULA NAME/ {r=$4;f=$2} /ATTRIBUTE_INSTANCE NAME="Instance I'm Looking For"/ {} /CV/&&r&&f{c=$2;if(c!=""){print r" "f" "c;r="";f="";c=""}}

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk nested looping?

I am trying to parse a text file and send its output to another file but I am having trouble conceptualizing how I am supposed to do this in awk. The text file has a organization like so: Name Date Status Location (city, state, zip fields) Where each of these is on a separate line in... (1 Reply)
Discussion started by: kellyanneghj
1 Replies

2. Shell Programming and Scripting

Help on looping using awk

I have the data like this: PONUMBER,SUPPLIER,LINEITEM,SPLITLINE,LINEAMOUNT,CURRENCY IR5555,Supplier1,1,1,83.1,USD IR5555,Supplier1,1,3,40.4,USD IR5555,Supplier1,1,6,54.1,USD IR5555,Supplier1,1,8,75.1,USD IR5556,Supplier2,1,1,41.1,USD IR5556,Supplier2,1,3,43.1,USD ... (3 Replies)
Discussion started by: jeffreybsu
3 Replies

3. Shell Programming and Scripting

Looping through pairs of files with awk

Hi all, please help me construct the command. i want to loop through all files named bam* and bed*. My awk works for a particular pair but there are too many pairs to do manually. I have generated multiple files in a folder in a given pattern. The files are named like bam_fixed1.bam... (2 Replies)
Discussion started by: newbie83
2 Replies

4. Shell Programming and Scripting

AWK looping over 2 variables

I would like to loop over variables i and j consecutively, { a = -6.7 b = 7.0 c =0.1 { for (i = 0; i<=(b-a)/c; i++) for (j = 1; j<=(b-a)/c; j++) '$1<=(a+j*c)&&$1>=(a+i*c)' FILENAME > output_j '{print $2}' output_j > output_j_f } I essentially want to print the range of $1... (9 Replies)
Discussion started by: chrisjorg
9 Replies

5. Shell Programming and Scripting

Looping within the elements of a file using awk

Hi all, I have a file containing 5000 rows and 4 columns. I need to do a loop within the rows based on the values of column 3. my sample data is formatted like the ones below: what i need to do is to make a loop that will allow me to plot the values of x,y,values corresponding to month 1 to month... (10 Replies)
Discussion started by: ida1215
10 Replies

6. Shell Programming and Scripting

looping in awk

How do I remove last comma? echo "xx yy zz" | awk 'BEGIN{FS=" "}{for (i=1; i<=NF; i++) printf "%s,", $i}'output: xx,yy,zz, required output: xx,yy,zz or (ideally!): xx, yy & zz many thanks in advance! (4 Replies)
Discussion started by: euval
4 Replies

7. Shell Programming and Scripting

Urgent - Looping using AWK

Hi I have a file which is having following text. The file is in a tabular form with 5 fields. i.e field1, field2 ..... field5 are its columns and there are many rows in it say COUNT is the number of rows Field 1 Field2 Field3 Field4 Field5 ------- ------- ... (8 Replies)
Discussion started by: skyineyes
8 Replies

8. Shell Programming and Scripting

looping and awk/sed help

I am pretty new to this, but imagine what I am trying to do is possible iI am trying to make an automated DB comparison tool that selects all columns in all tables and compares them to the same thing in another DB. anyway I have created 2 files to help with this the first file is a... (13 Replies)
Discussion started by: Zelp
13 Replies

9. Shell Programming and Scripting

Awk: looping problem!

I am having a problem with awk when I run it with a loop. It works perfectly when I echo a single line from the commandline. For example: echo 'MFG009 9153852832' | awk '$2 ~ /^0-9]$/{print $2}' The Awk command above will print field 2 if field 2 matches 10 digits, but when I run the loop... (5 Replies)
Discussion started by: cstovall
5 Replies

10. UNIX for Advanced & Expert Users

Looping in awk

Can somebody give me a cleaner way of writing the following script. I was thinking that I could use a loop in the awk statement. It works fine the way it is but I just want the script to be cleaner. #!/usr/bin/sh for r in 0 1 2 3 4 5 6 do DAY=`gdate --date="${r} days ago" +%m\/%d\/%y`... (3 Replies)
Discussion started by: keelba
3 Replies
Login or Register to Ask a Question