Visit Our UNIX and Linux User Community


awk read input files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk read input files
# 1  
Old 09-09-2009
awk read input files

hi,

i'm a beginner in writing awk scripts and I have a problem with reading input files.
Requirement for my programm:
compare file1 to file2 and check if value in column1 is equal and value in column5 is different.
File 1:
Code:
180    P    01.01.2008    30.06.2008    2
180    P    01.07.2008    30.09.2008    1
180    P    01.10.2008    31.12.2008    1
1101    P    01.01.2008    30.11.2008    1
1101    P    01.12.2008    31.12.2008    1
1101    P    01.12.2008    31.12.2008    2
1101    P    01.12.2008    31.12.2008    3

File 2:
Code:
180    P    01.01.2008    30.06.2008    1
180    P    01.07.2008    30.09.2008    1
180    P    01.10.2008    31.12.2008    1
1101    P    01.01.2008    30.11.2008    1
1101    P    01.12.2008    31.12.2008    2

My Code:
Code:
BEGIN {
        SUBSEP=" "
        if (ARGC < 3) {
             print "gawk -f L16.awk ZZT_T5A71.txt T5A71.txt"
            exit
        } else {
            t5a71 = ARGV[2]
            zzt_t5a71 = ARGV[1]
        }
    
    }
{#MAIN
    if (FILENAME == zzt_t5a71) {
       split($0, record2, SUBSEP)    

    }
    if (FILENAME == t5a71) {
       
        split($0, record1, SUBSEP)
        
        Pernr = match(record1[1],record2[1])
        if (Pernr != 0) {
      
            Zeit = match(record1[3], record2[3])
            if (Zeit != 0){
              
                if (record1[5] > record2[5]){
                        arrGES[FNR] = $0
                    
                }
            }
        }
    }
}
END{
    for (x in arrGES)
        print arrGES[x]
    
}

My output is just
1101 P 01.12.2008 31.12.2008 3

and not
180 P 01.01.2008 30.06.2008 2
1101 P 01.12.2008 31.12.2008 3

Why??
# 2  
Old 09-09-2009
Hello,

Try this:
Code:
awk 'NR<=FNR{_f1[$1 $5]=1;next}!_f1[$1 $5]' file2 file1

# 3  
Old 09-09-2009
Thanks for the fast answer. I didn't expect that you can write this programm in one codeline. Can somebody explain me this codeline
Code:
awk 'NR<=FNR{_f1[$1 $5]=1;next}!_f1[$1 $5]' file2 file1

Thanx
Tgooper
# 4  
Old 09-10-2009
awk is known for being terse.

NR<=FNR that's a condition. If the total number of records processed so far is less or equal to the record number in the current file. In other words, all records from first file (file2).

{_f1[$1 $5]=1;next} if condition is met, we fill an associative array with, as index, a concatenation of field 1 and 5 from file2 and give it a value of 1 (true). When its done we skip to next line in the same file without executing the remaining awk instructions. You can see this as a loop on first file. At the end of the file, the condition above will be false and awk will continue on the second instruction bloc with the first record of second file (file1 in this case).

!_f1[$1 $5]' is short hand for !_f1[$1 $5]{print} again, <condition>{action} if the concatenation of field 1 and 5 from second file (file1) was seen in first file (file2), the array _f1 will have a value 1 (true) hence skip record. Otherwise, print.
# 5  
Old 09-10-2009
Thanks for the explanation of the code. Smilie
The output of the programm should be those lines where column5 is different. With your code I get file1 as output. Smilie
# 6  
Old 09-10-2009
Quote:
Originally Posted by tgooper
Thanks for the explanation of the code. Smilie
The output of the programm should be those lines where column5 is different. With your code I get file1 as output. Smilie

Code:
TEXTBOX>awk 'NR<=FNR{_f1[$1 $5]=1;next}!_f1[$1 $5]' file2 file1
180    P    01.01.2008    30.06.2008    2
1101    P    01.12.2008    31.12.2008    3

Don't know what Your expecting ?.
# 7  
Old 09-10-2009
Sorry had the wrong file for my test!

Previous Thread | Next Thread
Test Your Knowledge in Computers #106
Difficulty: Easy
A kernel is at the heart of the Linux OS; and this kernel acts as an interface between the user and underlying hardware.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. Shell Programming and Scripting

Parse input of two files to be the same in awk

I have two files that I am going to use diff to find the differences but need to parse them before I do that. I have include the format of each file1 and file2 with the desired output of each (the first 5 fields in each file). The first file has a "chr" before the # that needs to be removed. I... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

4. Shell Programming and Scripting

Read input file with in awk script not through command line

Hi All, Do we know how to read input file within awk script and send output toanother log file. All this needs to be in awk script, not in command line. I am running this awk through crontab. Cat my.awk #!/bin/awk -f function test(var){ some code} { } END { print"test code" } (5 Replies)
Discussion started by: random_thoughts
5 Replies

5. Shell Programming and Scripting

AWK using two input files

Hi , i have two input files one is input.gz and another is ( input.txt) text file.in gz format input file each record contains 10 fields and corresponding header value is present in the text file as a single record i.e text file contains only 10 records which is header value,so output of the awk... (1 Reply)
Discussion started by: Ajoy
1 Replies

6. Shell Programming and Scripting

Script to read input and output files and child scripts

I have a directory where i have *.sas; *.pl;*.sh and *.c scripts I need to find out what are the child scripts and input output files for each script: say I have a shell script which calls a perl script and a sas script: In my first line I want I a) the parent script name; b) the... (1 Reply)
Discussion started by: ramky79
1 Replies

7. Shell Programming and Scripting

Comparing 2 input files -Awk

Compare 2 files and print the values input1 (c1 20 100 X_y10) along with one closest highest (c1 100 200 X_y10) and one lowest values (c1 10 15 X_y10) from input2 input1 c1 20 100 X_y10 input2 c1 5 10 X_y10 c1 10 15 X_y10 c1 100 200 X_y10 c1 200 300 X_y10 output ... (8 Replies)
Discussion started by: bumblebee_2010
8 Replies

8. Shell Programming and Scripting

Splitting input files into multiple files through AWK command

Hi, I needs to split *.txt files from single directory depends on the some mutltiple input values. i have wrote the code like below for file in *.txt do grep -i -h "value1|value2" $file > $file; done. My requirment is more input values needs to be given in grep; let us say 50... (3 Replies)
Discussion started by: arund_01
3 Replies

9. Shell Programming and Scripting

read input from 2 files and print them in alternate columns

Hi Frnz Please help me out. I have two text files. A.txt one two three four B.txt Jan Feb Mar Apr I need the output as (1 Reply)
Discussion started by: sriram.s
1 Replies

10. Shell Programming and Scripting

2 input files for awk

Hi, i have 2 files like f1 and f2 f1: 1 Note: some times it will be cahnged to 2 and 3. f2: 1:20 2:30 4:50 6:70 8:90 3:20 1:30 1:40 output: 1:80 (sum of 1) (6 Replies)
Discussion started by: koti_rama
6 Replies

Featured Tech Videos