comparing column of two different files and print the column from in order of 2nd file

Hi friends,

My file is like:

Second file is :

I need to print the rows present in file one, but in order present in second file....I used

 
while read gh;do
awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile
done < secondfile

but the output I am getting is:

Whereas, i want to print it like:

 
A
B
ABSENT
D
E
F
ABSENT

Can any one help me in using the above awk command such that it prints the required output.

Try this

awk 'NR==FNR{_1[$0]++;next} {if(!_1[$0]){$0="ABSENT"} print }' file1 file2

regards,
Ahamed

Hi Ahamed,

that doesn't work.....
it is showing ABSENT for all the rows :frowning:

whereas, i want to print it like:

awk 'NR==FNR{a[$0]++;next}{if ($0 in a){print $0}else{print "ABSENT"}}' file1 file2
$ awk 'BEGIN { while(getline a<"file1") ARR[a]=1       }
        {       (ARR[$0]) || $0="ABSENT"        } 1' < file2
A
B
ABSENT
D
E
F
ABSENT
$

If file1 isn't in order, you have to store the whole thing to tell if lines are absent or present.

Using basic unix shell commands:

#!/usr/bin/ksh
while read mStr; do
  mCnt=$(egrep -c ${mStr} File1)
  if [[ "${mCnt}" = "0" ]]; then
    echo "ABSENT"
  else
    echo ${mStr}
  fi
done < File2

Hi all,

Thanx for the help.....

it is giving the correct output.... :slight_smile:

Furthermore, what should I do if my first file is having multiple columns, like:

and i want to arrange my data as in second file:

and want to print the 5th row of first file, in the order present in second file

required output should be:

awk 'NR==FNR{a[$1]=$5;next}{print a[$0]}' file1 file2

Thanks Bartus,

Sorry!!!! that i couldn't explain my question properly....

I actually want to ask that if my first file is like:

and second file is

then the output must be :

and if first file contains all the rows present in second file it will print the complete 5th row in order as that in 2nd file....

I apologize for my silly mistake.....

I'm having real trouble following your logic there. How does the order get reversed? Why does it print 5342 when 78, uio, and 56 are all absent? Do you really mean the 5th line and only the 5th line?

Try:

awk 'NR==FNR{a[$1]=$5;next}{print a[$0]?a[$0]:"ABSENT"}' file1 file2
1 Like

I think I get it...

awk -v DATA1="datafile1" 'BEGIN {
        COMPLETE=1
        # Read lines like 'fds 21 sdf 32 6432', so A["21"]="6432" etc.
        while(getline <DATA1)
        {
                for(N=1; N<NF; N++)     A[$N]=$NF
                # Save the fifth line.
                if((++L) == 5) LINE=$0;
        }
}

{
        if(A[$1])       print A[$1];
        else
        {
                COMPLETE=0; # Remember if any fields were missing.
                print "ABSENT";
        }
}

END {
        # If no fields were missing, print the complete fifth line.
        if(COMPLETE)    print LINE;
}' < datafile2
1 Like

Hey corona......:slight_smile:

Thanks a lot....it works well :smiley:

But, I didnt understand that which statement is storing the 5th column of first file.....although u mentioned "# Save the fifth line"....... I tried by replacing 5 by 3 or 2 in

....but it is still giving the same output as in

, I need to store 2nd and 5th column data in separate files.

Also, my secondfile has 20 rows of strings, whereas in everyoutput, it is showing 21 rows.....last row is always "ABSENT", all above 20 rows are in desired way..... can u plz figure it out. :confused:

---------- Post updated at 12:20 PM ---------- Previous update was at 10:57 AM ----------

Hey Bartus :slight_smile:

ur code also works well.......

But, this is also showing the same problem of printing one extra last row printed ABSENT.

i.e, the second file has 20 rows, and first file has 20 or less rows.....so expected outcome is 20 rows only...but instead of that it is showing 21 rows and every time last row is showing "ABSENT"

Can you post output of

cat -e file2

?

kudos to u bartus :b:....
u caught it and I got it.....:stuck_out_tongue:

my second file was having one extra blank row at the last line.

I corrected it.....and now getting the perfect result.....:smiley: