LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-12-2012, 01:19 AM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
diff $Var1 $Var2 ?


Hi, I am looking for a solution for comparing two variables without using a tmp file (written on hdd), and only work with variables. Target: I simply would like to output the new lines that have been seen into $Var2.

sthg like this for instance.
Code:
diff -someoptions $Var1 $Var2
Until now, I use this method (that I do not favor), because ram can do it as well
Code:
comm --nocheck-order  -3  $file1 $file2
with file1=`cat $Var1` and file2=`cat $Var2`

Maybe you have a trick ...
 
Old 01-12-2012, 01:30 AM   #2
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
Depending on whats in the variables you could do it with a simple if statement.

Code:
~/tmp $ echo $var1 $var2 $var3
abc abc zxc
~/tmp $ if [ $var1 != $var2 ]; then echo "$var1 : $var2"; fi
~/tmp $ if [ $var1 != $var3 ]; then echo "$var1 : $var3"; fi
abc : zxc
~/tmp $
 
Old 01-12-2012, 01:40 AM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Code:
#!/bin/bash

Var1=$( /bin/cat << EOF
Some text
more text
Even more text
EOF
)

Var2=$( /bin/cat << EOF
Some text
more text
New text here
Even more text
And even more new text
EOF
)

/usr/bin/mkfifo fifo1 fifo2

echo "${Var1}" > fifo1 &
echo "${Var2}" > fifo2 &

/usr/bin/diff fifo1 fifo2

/bin/rm fifo1 fifo2
From man 7 fifo:
Code:
A FIFO special file (a  named pipe) is similar to a pipe, except that it is accessed as part of the
file system.  It can be opened by multiple processes for reading or writing.  When processes are
exchanging data via the FIFO, the kernel passes all data internally without writing it to the file
system. Thus, the FIFO special file has no contents on the file system; the file system entry
merely serves as a reference point so  that  processes can access the pipe using a name in the
file system.
EDIT:
I initially thought of the if-statement approach too fukawi1, but it sounds like the OP wants to see the new lines of Var2--not to just visually compare their values.

Last edited by Dark_Helmet; 01-12-2012 at 01:43 AM.
 
1 members found this post helpful.
Old 01-12-2012, 02:19 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Instead of explicitly creating and removing the FIFOs, you can use process substitution:
Code:
diff <(echo "$Var1") <(echo "$Var2")
 
2 members found this post helpful.
Old 01-12-2012, 03:24 PM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Much more compact. Kudos. I thought there would be something like that, but couldn't remember it.
 
Old 01-12-2012, 06:40 PM   #6
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Moderator response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 01-12-2012, 09:56 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
It's hard to say without some more context, but since you mention "adding new lines", it sounds like you might possibly want to consider using arrays instead.
 
Old 01-13-2012, 01:13 PM   #8
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
thank you for ur coding interests

lets take 1 example:

let var1 contains

Quote:
A.txt
B
C
D
E
let var2 with
Quote:
C
D
E
F
G
H
my dreamt ouput would be after the kinda diff:
Quote:
A.txt
B
F
G
H
if it would be possible eventually....
 
Old 01-13-2012, 02:49 PM   #9
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Code:
$ join -v1 -v2 <(echo "$var1") <(echo "$var2")
A.txt
B
F
G
H
In your initial post you only refer to $var2 to be output, hence leave -v1 out in this case.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Trying to concatenate two variables and a slash ($var1/$var2). jbulgier Linux - General 2 12-13-2011 10:52 PM
echo "Here I am in $var1 selling ties": does not do what I expect. stf92 Programming 5 07-07-2011 10:52 PM
how to send the output of a command in a file defined by a eval $"$var1" toordog Programming 3 07-07-2010 04:26 PM
bash: how to divide complex : FINAL=`[ ($var2 - $var1 ) / $var1 * 100 ]` frenchn00b Programming 9 11-06-2008 10:04 AM
Dual Boot diff Hard Disk diff OS on Suse 9.1 wilhem Linux - Newbie 1 08-13-2004 06:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration