Simple comparison between two lists.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple comparison between two lists.
# 1  
Old 02-25-2014
RedHat Simple comparison between two lists.

I have two lists (input) Alpha and Beta. [Values are tab separated. Now i am showing it separated with = sign]

Alpha:
Quote:
x=1
y=2
z=3


Beta:

Quote:
y=2
a=1
x=1
z=4
Need the output like this:

I would like to get an output like this:
Alpha vs Beta
Code:
| --  | a=1 |
|z=3  | z=4 |

Is it possible ? Smilie
# 2  
Old 02-25-2014
Hello,

Following may help you in same.

Code:
$ cat differ_2_files121111.ksh
a=`awk 'NR==FNR {a[$1];next} !($1 in a) {print $1}' check_file_comp2 check_file_comp1`
b=`awk 'NR==FNR {a[$1];next} !($1 in a) {print $1}' check_file_comp1 check_file_comp2`
echo "Alpha vs. Beta "
echo $a "| " $b

Output will be as follows.

Code:
Alpha vs. Beta
z=3 |  a=1 z=4


NOTE: Where check_file_comp1(alpha) and check_file_comp2(Beta) are input files.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 02-25-2014
Try also
Code:
awk     'NR == FNR      {T[$1]=$2;next}
         $2 == T[$1]    {delete T[$1]; next}
                        {printf "| %s=%s |",$1,$2;
                         if (T[$1]!="") printf " %s=%s |\n", $1, T[$1]
                         else           printf "  -- |\n"
                         delete T[$1]
                        }
         END            {for (i in T) printf "|  -- | %s=%s |\n", i, T[i]}
        ' file1 file2
| a=1 |  -- |
| z=4 | z=3 |

These 3 Users Gave Thanks to RudiC For This Post:
# 4  
Old 02-25-2014
Quote:
Originally Posted by RudiC
Try also
Code:
awk     'NR == FNR      {T[$1]=$2;next}........
| a=1 |  -- |
| z=4 | z=3 |

It seems I am getting a different output in this..
can u see this screenshot http://awesomescreenshot.com/0fc2em427d
(Edited the screenshot link)

---------- Post updated at 08:37 PM ---------- Previous update was at 08:33 PM ----------

Quote:
Originally Posted by RavinderSingh13
Hello,

Following may help you in same.

Code:
$ cat differ_2_files121111.ksh
a=`awk 'NR==FNR {a[$1];next} !($1 in a) {print $1}' check_file_comp2 check_file_comp1`
b=`awk 'NR==FNR {a[$1];next} !($1 in a) {print $1}' check_file_comp1 check_file_comp2`
echo "Alpha vs. Beta "
echo $a "| " $b

Output will be as follows.

Code:
Alpha vs. Beta
z=3 |  a=1 z=4

NOTE: Where check_file_comp1(alpha) and check_file_comp2(Beta) are input files.


Thanks,
R. Singh
Ravinder Sing,
This works fine. But my data is little long. (not short as the given example). So I am finding problem using this as it will list all the data in one line !

Real Example
Code:
xxxxxxxxxxxxxxxx=11111111111111111111111111111
yyy=66666666666666666666666666666666666666666
zzzzzzzzzzzzzz=uuuuuuuuuuuuuu8888d8fd8fdfd


Last edited by linuxadmin; 02-26-2014 at 12:26 AM.. Reason: code tags
# 5  
Old 02-25-2014
I can't see your screenshot. What be the difference in the outputs?
# 6  
Old 02-25-2014
Some tweaks to RudiC's solution:

Code:
awk '
  NR==1{ printf "%s vrs. ",FILENAME }
  NR==FNR{T[$1]=$2;next}
  FNR==1{ print FILENAME }
  {
    if ($2 != T[$1]) {
        if(T[$1] == "") printf "|  --"
        else printf "| %s=%s", $1, T[$1]
        printf " | %s=%s |\n",$1,$2;
    }
    delete T[$1]
  }
  END {for(i in T) printf "| %s=%s |  -- |\n", i, T[i]}' alpha beta
alpha vrs. beta
|  -- | a=1 |
| z=3 | z=4 |

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

2. Shell Programming and Scripting

get the lists

I expert, I may cross post something similar but I dirtyed my quesion somehow to be clear in the thread #cat file1 88dee gcc: Grok for callconvention-hard to enable hard float a2ad2 eglibc: package mtrace separately 61487 python: bump PR of packages after update of distutils.bbclass... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

3. Shell Programming and Scripting

Need help with simple comparison in AWK

Hi, I'm new to AWK and I'm having problems comparing a field to a string variable. /ARTIST/ {x = $2} $1 ~ x {print $0}My code tries to find a record with the string "ARTIST". Once it finds it, it stores the second field of the record into a variable. I don't know what the problem is for the... (7 Replies)
Discussion started by: klusps
7 Replies

4. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

5. Shell Programming and Scripting

combining two lists

Hi, So I I received two lists for my merchandise and both are similar but differences do occur. I want to combine two lists that have similar names but I dont want the similar name to come up twice because I will end up purchasing two of those items. Heres an example below (file is massive). ... (1 Reply)
Discussion started by: kylle345
1 Replies

6. Shell Programming and Scripting

Comparison between 2 large lists with Getting VALUES from one into the other

hi, I have 2 large lists: LIST A: containes 6 fields of many entries (VARIABLE number), like: 2011-07-10 | 18:19:47 | 38037300 | 9647808003122 | 2 | success LIST B: containes 3 fields & 183 entries (FIXED number), like: 9647805651885 9647805651885 SCP_10 What I want is a... (8 Replies)
Discussion started by: amurib
8 Replies

7. Shell Programming and Scripting

Shell Script to Create non-duplicate lists from two lists

File_A contains Strings: a b c d File_B contains Strings: a c z Need to have script written in either sh or ksh. Derive resultant files (File_New_A and File_New_B) from lists File_A and File_B where string elements in File_New_A and File_New_B are listed below. Resultant... (7 Replies)
Discussion started by: mlv_99
7 Replies

8. Shell Programming and Scripting

Simple XML file comparison and merging

Okay, first of all, thanks to everyone who's helped me out before... I appreciate the opportunity to learn. I have two iTunes XML files, and I simply want to compare the contents, then merge. Theoretically, this will allow me to merge two libraries, keeping playlists intact (depending on iTunes'... (4 Replies)
Discussion started by: karlp
4 Replies

9. Shell Programming and Scripting

How to get the files lists

Hi All, Need the help in getting the file list which are generated for the time period. example if i want to get the list of file generated between 11 to 12 clock. i used the find command search the files with -cmin flag with -60. find /home/test/* -cmin -60 -type f -exec ls {} \; ... (2 Replies)
Discussion started by: nmadhuhb
2 Replies
Login or Register to Ask a Question