Operating System - Linux
1748181 Members
4131 Online
108759 Solutions
New Discussion юеВ

Re: how to paser this string

 
SOLVED
Go to solution
Gemini_2
Regular Advisor

Re: how to paser this string

Actually, I parse the string because I want to do the following.

I want to diff two directories, and only display the "Only In" result.

Is there an option in the diff that can do that? so, I dont even have to parse the result.

Mimosa
James R. Ferguson
Acclaimed Contributor

Re: how to paser this string

Hi (again) Gemini:

> I want to diff two directories, and only display the "Only In" result

See the manpages for 'dircmp' :

http://docs.hp.com/en/B2355-60105/dircmp.1.html

Regards!

...JRF...
Sandman!
Honored Contributor

Re: how to paser this string

Not sure I understand...could you post the diff(1) command you are using?
A. Clay Stephenson
Acclaimed Contributor

Re: how to paser this string

It would have really helped had you asked the real question rather than leading everyone of a Wild Goose chase. I think Hein alluded to this earlier. Anyway, you can leverage the comm command suppressing the entries found in both directories.

Something like this:
---------------------------------------
#!/usr/bin/sh

typeset TDIR=${TMPDIR:-/var/tmp}
typeset PROG=${0##*/}
typeset T1=${TDIR}/X${$}_1.tmp
typeset T2=${TDIR}/X${$}_2.tmp

trap 'eval rm -f ${T1} ${T2}' 0 1 2 15

typeset -i STAT=0

if [[ ${#} -ge 2 ]]
then
typeset D1=${1}
typeset D2=${2}
shift 2
if [[ -d "${D1}" && -d "${D2}" ]]
then
ls D1 | sort > ${T1}
ls D2 | sort > ${T2}
comm -3 ${T1} ${T2}
STAT=${?}
else
echo "${PROG}: ${D1} and/or ${D2} not found and/or not directories." >&2
STAT=254
fi
else
echo "Usage: ${PROG} dir1 dir2" >&2
STAT=255
fi
exit ${STAT}
-----------------------------------------

If I didn't make no typing booboo's; that should work. Use it like "dirdiff.sh dir1 dir2". Files that are unique to dir1 appear in column1 of stdout and files unique to dir2 appear in column2.

Man comm for details. You could easily separate this into 3 output files but that is left for you.

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: how to paser this string

Ooops, I'm stupid I did have a minor error, D1 rather than ${D1}.
----------------------------------------
#!/usr/bin/sh

typeset TDIR=${TMPDIR:-/var/tmp}
typeset PROG=${0##*/}
typeset T1=${TDIR}/X${$}_1.tmp
typeset T2=${TDIR}/X${$}_2.tmp

trap 'eval rm -f ${T1} ${T2}' 0 1 2 15

typeset -i STAT=0

if [[ ${#} -ge 2 ]]
then
typeset D1=${1}
typeset D2=${2}
shift 2
if [[ -d "${D1}" && -d "${D2}" ]]
then
ls ${D1} | sort > ${T1}
ls ${D2} | sort > ${T2}
comm -3 ${T1} ${T2}
STAT=${?}
else
echo "${PROG}: ${D1} and/or ${D2} not found and/or not directories." >&2
STAT=254
fi
else
echo "Usage: ${PROG} dir1 dir2" >&2
STAT=255
fi
exit ${STAT}


-----------------------------------------

Dircmp may be too intensive for you because it not only does file names but also compares the files themselves.
If it ain't broke, I can fix that.
Gemini_2
Regular Advisor

Re: how to paser this string

No....I dont want to waste everyone's time.

I want to learn different ways to parse the strings anyway. I read every post and try to understand it.

you guys are being extremely helpful.


really appreciate.
Sandman!
Honored Contributor

Re: how to paser this string

Imho if the diff(1) you are using is constructed as shown...

# VAR=$(diff /dir1/dir2 /dir1/dir3)

then echo'ing the doubly-quoted VAR will do the trick w/o any scripting...

# echo "$VAR"

~hope it helps
Arturo Galbiati
Esteemed Contributor

Re: how to paser this string

Hi,
why not the old good Unix command diff?

/> ls -1 t1
a
b

/>ls -1 t2
b
c

/> diff t1 t2
Only in t1: a
Only in t2: c

It shows only the file that are not in both dirs.


HTH,
Art