awk - (URGENT!) Print lines sort and move lines if match found


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk - (URGENT!) Print lines sort and move lines if match found
# 1  
Old 02-02-2015
awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!!

I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but

- Currently, script is not printing lines to File3.txt in order.
- Also the matching lines are not moving out of File1.txt

Thanks a lot.
Any help is appreciated.

Script I am using:

Code:
     awk '
      BEGIN {
        OFS="\t"
        out = "File3.txt"  }
    NR == FNR {
    	if(NF)
    		a[substr($0,1,14)]=$0;next
    	next
    }
    function print_77_99() {
    	if(key in a)
    		print "77", a[key] > out
    }
    
      $1 == "01" {
        if (FNR > 1) print_77_99()
        lines = ""
      }
    $1=="01" &&
                FNR>1       {keyfound?"77\t" keyfound:"99"}
             $1=="01"       {PK=$4 $3 $2
                             keyfound=""
                            }
             $1=="07"       {key=PK $4}
             key in a        {keyfound=a[key]}
    
    { if(key in a)
    		print $0 > out
    lines = lines $0 "\n"
    }
    
    END {	print_77_99()
    }' File2.txt File1.txt

File1.txt

Code:
 01  89  68  5000
    02  83  11
    04  83  9   02
    03  83  00
    06  83  00
    07  83  11  RT0429
    07  83  88  FS0547
    01  44  73  8800
    02  44  73
    04  44  73   02
    03  44  73
    06  44  73
    07  44  11  RT  0789

File2.txt

Code:
    50006889RT0429 NARD /3010  /E     /C A87545457          /  //                ///11        ///
    51002387 NARD /3000  /E     /S N054896334IV          /  //                ///11        ///

Current Output (File3.txt)

Code:
    07  83  11  RT0429
    77	50006889RT0429 NARD /3010  /E     /C A87545457          /  //                ///11        ///
    01  44  73  8800
    02  44  73
    04  44  73   02
    03  44  73
    06  44  73

Desired Output (File3.txt)

I need complete set from line 01 to 07 including any addition 07 lines in case the key is matched from File1, followed by 77 tab and matched string from File2.

Code:
 01  89  68  5000
    02  83  11
    04  83  9   02
    03  83  00
    06  83  00
    07  83  11  RT0429
    07  83  88  FS0547
    77	50006889RT0429 NARD /3010  /E     /C A87545457          /  //                ///11        ///

Desired Format(File1.txt)

I need the matched records out of the File1.txt (remove) so the following is not matched record that would stay in File1.txt till a match is found.

Code:
    01  44  73  8800
    02  44  73
    04  44  73   02
    03  44  73
    06  44  73
    07  44  11  RT  0789

# 2  
Old 02-02-2015
Moderator's Comments:
Mod Comment As has been explained in the Rules you agreed to when you joined the UNIX and Linux Forums and in private mail, these forums are responded to by volunteers; not paid staff. You can purchase a higher priority for your thread with points by posting to the Emergency UNIX and Linux Support forum. Demanding URGENT support in other forums is not allowed.

And, this is a duplicate of another thread you started a few hours earlier: awk - Print lines if only matching key is found and duplicate threads are not allowed.

This thread is closed.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine lines if fields match in lines

In the awk below, what I am attempting to do is check each line in the tab-delimeted input, which has ~20 lines in it, for a keyword SVTYPE=Fusion. If the keyword is found I am splitting $3 using the . (dot) and reading the portion before and after the dot in an array a. If it does have that... (12 Replies)
Discussion started by: cmccabe
12 Replies

2. UNIX for Dummies Questions & Answers

awk - Print lines if only matching key is found

I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. Thanks a lot. Any help is appreciated. Script I am using: awk 'FNR == NR && ! /^]*$/ {... (9 Replies)
Discussion started by: High-T
9 Replies

3. Shell Programming and Scripting

awk print pattern match line and following lines

Data: Pattern Data Data Data Data Data Data Data Data Data ... With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line. (9 Replies)
Discussion started by: dmesserly
9 Replies

4. Shell Programming and Scripting

awk to print all lines after a pattern is found

Is there a way with aw to print all lines after a string is found There is a file like this ....... ........ 2012/19/11 :11.58 PM some data lne no date 2012/19/11 :11.59 PM some other data 2012/20/11 :12.00 AM some other data some line without dates some more lines without dates... (8 Replies)
Discussion started by: swayam123
8 Replies

5. UNIX for Dummies Questions & Answers

Display n lines after match found and other line

I have a file like this DoctorName Address1 Address2 DOB InsuredName Address1 Address2 DOB PatientName Address1 Address2 DOB ClaimNo1 DoctorName Address1 Address2 DOB InsuredName (2 Replies)
Discussion started by: nsuresh316
2 Replies

6. UNIX for Dummies Questions & Answers

Display n lines before match found

I have a file with following data A B C D E F G H I K L M N and search pattern is G Expected output (3 Replies)
Discussion started by: nsuresh316
3 Replies

7. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

8. UNIX for Dummies Questions & Answers

Awk print all lines on match?

Ok so I can use awk to match a pattern and print the whole line with print $0. Is there any way to just tell awk to print every line of output when the pattern matches? I'm having it wait for the word error and then print that entire line. But what I actually need to see is all the following... (9 Replies)
Discussion started by: MrEddy
9 Replies

9. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies

10. UNIX for Dummies Questions & Answers

Grep and display n lines after the match is found.

Hello, How do I use grep to find a pattern in a list of file and then display 5 lines after the pattern is matched Eg: I want to match the string GetPresentCode in all files in a folder and then see 4 lines following this match. I am not sure if grep is what should be used to achieve. Thanks!... (3 Replies)
Discussion started by: cv_pan
3 Replies
Login or Register to Ask a Question