Re: exchange some string in two files?





On 3/7/2008 2:15 AM, David.Zhuang@xxxxxxxxx wrote:
On 3月7日, 下午3?54分, mcolinc <shibin0...@xxxxxxxxxxx> wrote:

On 3月7日, 上午11时43分, David.Zhu...@xxxxxxxxx wrote:


Hello,

is there any eazy way by sh for following things?

1. two files; FILEA, FILEB
2. exchange everyline of char 200-250 on FILEA with everyline of char
300-350 on FILEB

could you help me to write the shell script for the job, thanks!

Is there any corelation between FILEA and FILEB?



there is no ovbious corelation between FILEA and FILEB.
just some data generate to the wrong file during mass output.
FILEA's char 200-250 should be in FILEB 300-350 and vice versa in each
line.

I am thinking should I extract both files' char other than 200-250 and
output to the other fine for combine?

If the files aren't huge, this approach should work:

$ cat FILEA
0123456789
1234567890

$ cat FILEB
abcdefghijklm
nopqrstuvwxyz

$ awk 'NR==FNR{a[NR]=substr($0,3,4);next}{$0=substr($0,1,2)a[FNR]substr($0,4)}1'
FILEA FILEB
ab2345defghijklm
no3456qrstuvwxyz

$ awk 'NR==FNR{a[NR]=substr($0,3,4);next}{$0=substr($0,1,2)a[FNR]substr($0,4)}1'
FILEB FILEA
01cdef3456789
12pqrs4567890

Just replace the start and range values on each of the substr()s as appropriate
to suit your requirements, e.g.:

awk
'NR==FNR{a[NR]=substr($0,200,50);next}{$0=substr($0,1,299)a[FNR]substr($0,351)}1'
FILEA FILEB

awk
'NR==FNR{a[NR]=substr($0,300,50);next}{$0=substr($0,1,199)a[FNR]substr($0,251)}1'
FILEB FILEA

(check the numbers!).

Ed.

.



Relevant Pages