0

I have two text files

cat A.txt

10,1,1,"ABC"
10,1,2,"S1"
10,1,2,"ABC"
10,1,3,"baba"
10,2,1,"S2"
10,2,1,"asd"
10,2,2,"S3"
10,2,2,"dkkd"
10,2,3,"ABC"

cat B.txt

10,1,1,"ABC1"
10,1,2,"S1"
10,1,2,"ABC"
10,1,3,"baba"
10,2,1,"asd"
10,2,2,"S3"
10,2,2,"dkkd"
10,2,4,"bokaj"

I want to find the missing fields by reading from two text files and fill up in both the files for missing fields by " " and save to two new modified files How do i get this say

A1.txt is a modified version of A.txt

cat A1.txt

10,1,1,"ABC"
10,1,2,"S1"
10,1,2,"ABC"
10,1,3,"baba"
10,2,1,"S2"
10,2,1,"asd"
10,2,2,"S3"
10,2,2,"dkkd"
10,2,3,"ABC"
10,2,4,"  "

B1.txt is a modified version of B.txt

cat B1.txt

10,1,1,"ABC1"
10,1,2,"S1"
10,1,2,"ABC"
10,1,3,"baba"
10,2,1,"  "
10,2,1,"asd"
10,2,2,"S3"
10,2,2,"dkkd"
10,2,3,"  "
10,2,4,"bokaj"

make sure that total number of lines in A1.txt is same as that of B1.txt, i am new to bash, your answer with explaination may help me to learn this alot.

This is my MWE which i have tried so far

#!/bin/bash


cut -d ',' -f1,2,3 A.txt > A1.txt
cut -d ',' -f1,2,3 B.txt > B1.txt


## Command to print contents which are in B1.txt but not in A1.txt

A=`awk 'NR==FNR{a[$0];next} !($0 in a)' A1.txt B1.txt`
echo $A,'" "' >> A.txt
sort A.txt

## Command to print contents which are in A1.txt but not in B1.txt

B=`awk 'NR==FNR{a[$0];next} !($0 in a)' B1.txt A1.txt`
echo $B,'" "' >> B.txt
sort B.txt
  • 1
    elaborate more. What do you mean by missing field ? – Sagar Jun 19 at 14:54
  • do you mean missing field or missing row ? – Sagar Jun 19 at 15:08
  • This is going to be tricky because your files don't have a unique key. – glenn jackman Jun 19 at 15:12
0

Perhaps diff then sort can come in handy here.

For the A.txt and B.txt files with their respective companions A1.txt and B1.txt files already set up as in your example, do:

diff --unchanged-line-format= --old-line-format= --new-line-format='%l,"  "'%c\'\\12\' A1.txt B1.txt | sort -st , -k 1,3 A.txt -

and:

diff --unchanged-line-format= --old-line-format= --new-line-format='%l,"  "'%c\'\\12\' B1.txt A1.txt | sort -st , -k 1,3 B.txt -

These should yield the output you described.

0
grep -vFf B.txt A.txt | sed 's/".*"/" "/' |  sort -st, -k1,3 - B.txt

result (B1.txt):

10,1,1," "
10,1,1,"ABC1"
10,1,2,"S1"
10,1,2,"ABC"
10,1,3,"baba"
10,2,1," "
10,2,1,"asd"
10,2,2,"S3"
10,2,2,"dkkd"
10,2,3," "
10,2,4,"bokaj"

The 1st line is different from you sample, but i think it should be there as ABC is different from ABC1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.