grep dir2)

问题描述 投票:0回答:1
I also want these DISP.case_sc.in.YYY files in diffdir. Here YYY are the number of files that are equal to the difference m-n. Example if m=020 and n=010 so this YYY will vary from 011 to 020.

In both the dirs (1&2) some files will be exactly same that are not printed with the diff command, I need those files in a another directory name nodiffdir.

Sample input with files named to indicate their relationship to each other:

Expected output (dir1 and dir2 left unchanged):

diff -r dir1 dir2 |  grep dir2

diff -r dir1/DISP.case_sc.in.XXX dir2/**DISP.case_sc.in.XXX**

I have two directories dir1 and dir2. Both of them has n and m text files (n>m or n>m). I want to diff the files in both the directories and want to store the diffed files in a new directory. I used ...

This might work for you (GNU sed):

Only in dir2: DISP.case_sc.in.YYY

Make a new directory

Diff the two directories

and

dir1
    dir1_only
    comm_diff
    comm_same

dir2
    dir2_only
    comm_diff
    comm_same

and pipe the output to a sed invocation.

samedir
    comm_same   # copied as-is from dir1 or dir2

diffdir
    comm_diff   # output of "diff dir1/comm_diff dir2/comm_diff"
    dir1_only   # copied as-is from dir1
    dir2_only   # copied as-is from dir2
Convert each line that contains
awk sed diff
1个回答
1
投票

command:

mkdir dir3
diff -r dir1 dir2 |
sed '/dir2/!d;s#: #/#;s/.* //;s/.*/cp & &/;s/dir2/dir3/2'

dir3

dir1.dir2.

.dir2.

.e

mkdir dir3
diff -r dir1 dir2 |
sed '/dir2/!d;s#: #/#;s/.* //;s/.*/cp & &/;s/dir2/dir3/2;e'
我有两个目录,dir1和dir2中分别有m和n个文件,条件可能是m>n或m。

1
投票
$ cat tst.sh
#!/usr/bin/env bash

mkdir -p samedir &&
cp -r dir1 diffdir &&
cp dir2/* diffdir &&
for f1 in dir1/*; do
    fname="${f1##*/}"
    f2="dir2/${fname}"
    if [ -f "$f2" ]; then
        if diff -- "$f1" "$f2" > "diffdir/${fname}"; then
            cp -- "$f1" samedir &&
            rm "diffdir/${fname}"
        fi
    fi
done

所以,diffdir应该只有两个目录中不同的文件。

$ mkdir dir1
$ mkdir dir2
$ echo 'a' > dir1/dir1_only
$ echo 'a' > dir2/dir2_only
$ echo 'b' > dir1/comm_same
$ echo 'b' > dir2/comm_same
$ echo 'c' > dir1/comm_diff
$ echo 'd' > dir2/comm_diff

我使用了

$ ls *
tst.sh

dir1:
comm_diff   comm_same   dir1_only

dir2:
comm_diff   comm_same   dir2_only

它给了我两个目录中不同的文件

$ ./tst.sh

其中XXX可能从001到999不等(我在这里手动加了**,使名称变粗).我只需要在diffdir中的文件(粗体)。

$ ls *
tst.sh

diffdir:
comm_diff   dir1_only   dir2_only

dir1:
comm_diff   comm_same   dir1_only

dir2:
comm_diff   comm_same   dir2_only

samedir:
comm_same

由于两个目录中的文件数量不一样,所以在m>n的情况下,它也给出了上述命令的输出(diff -r dir1 dir2)。

© www.soinside.com 2019 - 2024. All rights reserved.