How to Do a One Way Diff in Linux

How do I do a one way diff in Linux?

diff A B|grep '^<'|awk '{print $2}'

grep '^<' means select rows start with <

awk '{print $2}' means select the second column

One-way diff file

back in the old days, before Vim, there used to be a line-oriented UNIX editor called 'ed' ..

diff has an option built in ( -e option ) , with which you can create an edit script from the diff.

Check here: and look for the section "Edit Script"

http://en.wikipedia.org/wiki/Diff

http://docs.freebsd.org/info/diff/diff.info.ed_Scripts.html

here's an example:

http://www.araxis.com/merge/topic_diff_report_editscript.html


another way to do this is to create a patch file (see 'man patch')

unix diff side-to-side results?

From man diff, you can use -y to do side-by-side.

-y, --side-by-side
output in two columns

Hence, say:

diff -y /tmp/test1  /tmp/test2

Test

$ cat a                $ cat b
hello hello
my name my name
is me is you

Let's compare them:

$ diff -y a b
hello hello
my name my name
is me | is you

Manually merge two files using diff

"I want to output the entire file in a unified format. Is there any way diff can do this?"

Yes.

diff -U 9999999 file1.txt file2.txt > diff.txt

This should work, provided your files are less than 10 million lines long.

diff to output only the file names

From the diff man page:

-q   Report only whether the files differ, not the details of the differences.

-r   When comparing directories, recursively compare any subdirectories found.

Example command:

diff -qr dir1 dir2

Example output (depends on locale):

$ ls dir1 dir2
dir1:
same-file different only-1

dir2:
same-file different only-2
$ diff -qr dir1 dir2
Files dir1/different and dir2/different differ
Only in dir1: only-1
Only in dir2: only-2

How to get the difference (only additions) between two files in linux

diff and then grep for the edit type you want.

diff -u A1 A2 | grep -E "^\+"

What is an easy way to do a sorted diff between two files?

This redirection syntax is bash specific. Thus it won't work in tcsh.

You can call bash and specify the command directly:

bash -c 'diff <(sort text2) <(sort text1)'

I have a file and I want to diff it against all files in another directory to find which one is the same

If your bash is not paleolithic, you can easily do that with pure bash:

shopt -s globstar
for f in bar/**/*; do
if [[ -f "$f" ]] && cmp -s foo "$f"; then
printf "The matched file is '%s'\n" "$f"
break
fi
done

The globstar option, which allows the use of bar/**/* to mean "all files in the directory tree headed by bar, was introduced in bash 4.0, released in 2009. (Unfortunately, that version is not installed by default on OS X.)

The break in the loop is an optimization which stops the search after one file is found, since the problem description clearly stated "one of the files". If multiple matches are possible and you need to know all of them, remove the break.



Related Topics



Leave a reply



Submit