Changed Field Number

HI,
I have two files and contains many Fields or columns with | (pipe) delimitor, wanted to compare both the files and get only unmatched perticular fields number.
ex:
first.txt

 1  |  2 | 3
111 |abc| 230 
hbc | bb2 | cs

second.txt

1  | 2   | 3
111 |abc |230 
abn | bb2 | fp

Here the different data in two fileds(i.e abn, fp in second file), the thing is i want only those changed fields or column numbers
The same scipt will be used for many files in my project so the field changes are happens any filed in the second file, so please provide the scipt or command with accurate result.
output.txt

1
3

Thanks.

so what have you tried so far?

btw, it seems like you recently have been asking the same questions repeatedly in some form or another. if so, maybe you need to understand the answers to the other threads first before asking the same question again.

2 Likes

Sorry i was trying many times previous method but i could not succeeded please provide another solution for the above thread.

Kindly respond ...

---------- Post updated at 06:18 AM ---------- Previous update was at 05:53 AM ----------

awk 'NR==1 {n=split($0,a," ")} NR==2 {split($0,b," ")} END {for (i=0;i++ <=n-1;) {if (a!=b) {print i;exit}}}' infile

This will compare only single file, but i wnated to compare two files and get unmatched field number in second file as mentioned above thread...please help thanks.

What be

? Does it mean fields 1 and 3 in row three? What if non-matches occur in other lines?

BTW - at least half the fields in your samples do not match.

Try:

awk -F\| '
  {
    split($0,F)
    getline<f
    for(i=1; i<=NF;i++) if($i!=F) C
  } 
  END{
    for(i in C) print i
  }
' f=file1 file2

are there really that many random spaces in your actual input file? If so try:

awk -F\| '
  {
    gsub(/[ \t]/,x)
    split($0,F)
    getline<f
    gsub(/[ \t]/,x)
    for(i=1; i<=NF;i++) if($i!=F) C
  } 
  END{
    for(i in C) print i
  }
' f=file1 file2

@Scrutinizer: What if the same field no. doesn't match in several lines? It would print the field no. just once, wouldn't it? Not sure if this be the desired behaviour...

As has already been noted, you have started multiple threads that seem to be asking the same question. When you are asked to clarify what your are trying to do, you open a new thread instead of answering questions that might enable us to figure out what you are trying to do.

In this thread you showed us first.txt and second.txt that both have 3 lines with three fields separated by "|" on each line. The first two fields of the first line of these files are different, the last two fields of the second line are different, and the first and third fields of the last line are different. Then you say that the result you want is:

1
3

We do not understand how six fields out of nine being different between your two input files results in the two lines of output you say you want!!!

If you want our help, please give a clear statement of what you are trying to do and a clear description of the output you are trying to produce.

Many thanks the code which have provided worked well...once agiain thanks a lot