开发者

script,unix,compare

开发者 https://www.devze.com 2022-12-08 03:55 出处:网络
I have two files ... file1: 002009092312291100098420090922111 010555101070002956200453T+00001190.81+00001295.920010.87P

I have two files ...

file1:

002009092312291100098420090922111
010555101070002956200453T+00001190.81+00001295.920010.87P
010555101070002956200449J+00003128.85+00开发者_JAVA技巧003693.90+00003128
010555101070002956200176H+00000281.14+00000300.32+00000281

file2:

002009092410521000098420090709111
010560458520002547500432M+00001822.88+00001592.96+00001822
010560458520002547500432D+00000106.68+00000114.77+00000106

In both files in every record starting with 01, the string from 3rd char to 25th char, i.e up to alphabet is the key.

Based on this key, I have to compare two files, and if there is any record matching in file 2, then I have to replace that record in file1, or else append it if it won't match.


Well, this is a fairly unspecific (and basic) programming question. We'll be better able to help us if you explain exactly what you did and where you got stuck.

Also, it looks a bit like homework, and people are wary of giving too much help on homework problems, as it might look like cheating.

To get you started:

  • I'd recommend Perl to solve this, but awk or another scripting language will also do. I'd recommend against sh/bash, as they are weak on text manipulation; also combining grep et al will become rather cumbersome.
  • First write a Perl program that filters records starting with 01. Then extract the key and put it into a hash (a Perl structure). Then output a new, combined file as required.


Using awk get the fields from 3-25 but doing something like awk -F "" '/^01/{print $1}' file_name | cut -c 3-25 and match the first two fields with 01 from both files and get all the lines in two different buffers and compare both the buffers using for line in in a shell script.

Whenever the line in second buffer matches the first one grep the line in second buffer in first file and replace the line in first file with the line in second. I think you need to work a bit around the logic.

0

精彩评论

暂无评论...
验证码 换一张
取 消