Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Awk: Performing "for" loop within text block with two files Post 303029186 by Don Cragun on Wednesday 23rd of January 2019 12:30:26 AM
Old 01-23-2019
Quote:
Originally Posted by Scrutinizer
You are welcome jvoot.

Don is right of course and it all depends what it is that you require.

To illustrate, to add the additional requirements, you could try these adjustments, which makes the code more precise, but more complex.
Code:
awk '
  NR==FNR {
    Keys[$1,$2]
    next
  }
  /<Co>/ { 
    for(line=1; line<=NF; line++) {
      split($line,Fields," ")
      if(line==1)
        subkey1=Fields[1]
      if(Fields[1]=="Lexeme") {
        for(i in Fields)
          if((subkey1,Fields[i]) in Keys) {
            print
            next
          }
        }
      }
    }
' file1 FS='\n' RS= ORS='\n\n' file2

How much precision you need depends on the variability of your input files. So to understand the limitations, you need to understand both your data and your code and you need to test of course.

This approach uses RS=, which is a special case, where an empty line (two consecutive newlines) is used as a record separator. The additional requirements, meant that the field separator FS needed to be changed to a newline, so that each field constitutes a line within a record in file2. These lines then needed to be split into smaller subfields using the split() command.

Each approach has its pros and cons.

A pro of this approach may be that the code can be simpler, so it may be easier to understand.

The cons are:
  • Adding more precision can sometimes lead to more complexity than a line based approach.
  • If there is so much as a space or TAB character on any of the empty lines, then it may break the solution..

You need to weigh these considerations when choosing your approach. It all depends..
Hi Scrutinizer,
You and I interpreted the requirement from post #1:
Quote:
My hope was that when $1 of File 1 matches $1 in File 2, $0 in File 2 contains the string "<Co>", and $2 of File 1 matches a string *exactly* in File 2 on a line beginning with the word "Lexeme," then print.
differently. You interpreted "when $1 of File 1 matches $1 in File 2, $0 in File 2 contains the string "<Co>"" to mean that "<Co>" has to appear somewhere in the multi-line record while I interpreted it to mean that "<Co>" has to appear in the line where $1 in File 1 matches $1 in File 2.

If we change the line in your code that is:
Code:
  /<Co>/ {

to be:
Code:
  $1 ~ /<Co>/ {

then I believe that your code and my code would produce the same results.

I also believe that changing the two lines in my code:
Code:
	if(PrintThisGroup) {
	... ... ...
++LinesInGroup == 1 && /<Co>/ {

to be:
Code:
	if(PrintThisGroup && Group ~ /<Co>/) {
	... ... ...
++LinesInGroup == 1 {

would make my code produce the same output as your code. I'm afraid jvoot will have to clarify the requirements for us to determine which one of us guessed correctly at the desired behavior.

Until I saw the sample record containing two "Lexeme" lines and two "ClauseType" lines, I had started trying a different approach (assuming that line 2 of a multi-line record in File 2 had to be the one and only "Lexeme" line in a record) that was closer to jvoot's original code. That method might have been easier for jvoot to follow, but since the given sample didn't match what I was expecting, I abandoned that approach.

It isn't surprising that we chose different approaches to solving the problem based on our different interpretations of the requirements. Clearly, there are lots of ways to address this problem.

Cheers,
Don
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing "END" before a new loop in AWK

First off, I have been learning AWK by trial and error over the last week or so, and there are some gaps in my basic understanding of the language. Here is my situation: I am coding and outputting results from an experiment I conducted in Psyscope, which has all been compiled into a master file.... (2 Replies)
Discussion started by: ccox85
2 Replies

2. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

3. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

4. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Get values from 2 files - Complex "for loop and if" awk problem

Hi everyone, I've been thinking and trying/changing all day long the below code, maybe some awk expert could help me to fix the for loop I've thought, I think I'm very close to the correct output. file1 is: <boxes content="Grapes and Apples"> <box No.="Box MT. 53"> <quantity... (8 Replies)
Discussion started by: Ophiuchus
8 Replies

7. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

8. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

9. Shell Programming and Scripting

For Loop Field editing - without using "awk"

Hi, I'm using Linux and bash shell. I have a file (F1.txt) with contents like Table1 Column1 123abc Table1 Column2 xyz Table2 Column1 543 Now, I would like to get the output as UPDATE Table1 SET Column1='123abc'; UPDATE Table1 SET Column2='xyz'; UPDATE Table2 SET Column1='543';... (3 Replies)
Discussion started by: Dev_Dev
3 Replies

10. Shell Programming and Scripting

Using "for" loop within "awk"

Hi Team. I am trying to execute a simple for loop within an awk but its giving a different result. Below is the main code: awk '{for(i=1;i<=6;i++) print $i}'The result should be 1 2 3 4 5 6 but its not giving this result. Can someone please help? (3 Replies)
Discussion started by: chatwithsaurav
3 Replies
All times are GMT -4. The time now is 01:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy