Comparing two files having different delimiters


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Comparing two files having different delimiters
# 1  
Old 11-04-2014
Comparing two files having different delimiters

hi,
i have a file called OVER90.txt and it's contents are as below:

over90.txt:
-------
Code:
UNIQUENAME 2013-12-06 11:23:48.1
UNIQUENAME2 2014-03-10 12:22:29.91
UNIQUENAME3 2013-04-02 10:41:22.1
UNIQUENAME4 2014-07-07 10:43:57.953

The ldap_jcon file's contents are as below:

ldap_jcon.txt
----------
Code:
UNIQUENAME,   HQ_USER, 
UNIQUENAME2,       DIST_AO, 
UNIQUENAME3,      DIST_AO, 
UNIQUENAME4,        HQ_USER, DIV_AO,

The output should be like:
Code:
UNIQUENAME 2013-12-06 11:23:48.1  - HQ_USER
UNIQUENAME2 2014-03-10 12:22:29.91  - DIST_AO
UNIQUENAME4 2014-07-07 10:43:57.953  - HQ_USER, DIV_AO

Note that i have different delimiter in the ldap_jcon file.
Is it possible to compare the two files based on the first field and create a new file the the output results shown above?





Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks

Last edited by vbe; 11-04-2014 at 02:32 PM..
# 2  
Old 11-04-2014
Try:
Code:
awk -F",? +" 'NR==FNR{x=$1;sub($1", *","",$0);a[x]=$0;next}{print $0" - "a[$1]}'  ldap_jcon.txt over90.txt

# 3  
Old 11-04-2014
@bartus11


I used this:
Code:
awk -F",? +" 'NR==FNR{x=$1;sub($1", *","",$0);a[x]=$0;next}{print $0" - "a[$1]}' ldap_jcon.txt over90.txt

The output was close, but after hyphen I got the username versus the roles that they had.

Data:
Code:
UNIQUENAME 2013-05-28 14:36:39.59 - UNIQUENAME
UNIQUENAME2 2012-11-29 09:17:06.992 - UNIQUENAME2
UNIQUENAME3 2014-07-18 09:18:24.249 - UNIQUENAME3
UNIQUENAME4 2011-08-12 20:52:13.424 - UNIQUENAME4

Thoughts?

Last edited by bartus11; 11-04-2014 at 04:30 PM.. Reason: Please use [code][/code] tags.
# 4  
Old 11-04-2014
Please use code tags as required by forum rules!

Try
Code:
awk 'FNR==NR{T[$1]=$0; gsub($1", *","",T[$1]); next} {print $0 " - " T[$1]}' FS=, file2 FS=" " file1
UNIQUENAME 2013-12-06 11:23:48.1 - HQ_USER, 
UNIQUENAME2 2014-03-10 12:22:29.91 - DIST_AO, 
UNIQUENAME3 2013-04-02 10:41:22.1 - DIST_AO, 
UNIQUENAME4 2014-07-07 10:43:57.953 - HQ_USER, DIV_AO,

This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-04-2014
Quote:
Originally Posted by RudiC
Please use code tags as required by forum rules!

Try
Code:
awk 'FNR==NR{T[$1]=$0; gsub($1", *","",T[$1]); next} {print $0 " - " T[$1]}' FS=, file2 FS=" " file1
UNIQUENAME 2013-12-06 11:23:48.1 - HQ_USER, 
UNIQUENAME2 2014-03-10 12:22:29.91 - DIST_AO, 
UNIQUENAME3 2013-04-02 10:41:22.1 - DIST_AO, 
UNIQUENAME4 2014-07-07 10:43:57.953 - HQ_USER, DIV_AO,

Perhaps a small addition to remove the training spaces and that pesky dangling `,'

Code:
wk 'FNR==NR{T[$1]=$0; gsub($1", *","",T[$1]); sub(/[, ]*$/, "", T[$1]); next} {print $0 " - " T[$1]}' FS=, ldap_jcon.txt FS=" " over90.txt
UNIQUENAME 2013-12-06 11:23:48.1 - HQ_USER
UNIQUENAME2 2014-03-10 12:22:29.91 - DIST_AO
UNIQUENAME3 2013-04-02 10:41:22.1 - DIST_AO
UNIQUENAME4 2014-07-07 10:43:57.953 - HQ_USER, DIV_AO

This User Gave Thanks to Aia For This Post:
# 6  
Old 11-04-2014
I thought of that as well; one gsub should suffice:
Code:
awk 'FNR==NR{T[$1]=$0; gsub($1", *|, *$","",T[$1]); next} {print $0 " - " T[$1]}' FS=, file2 FS=" " file1

This User Gave Thanks to RudiC For This Post:
# 7  
Old 11-05-2014
@RudiC - I used the code you provided and it gave me the same output as above.

Code:
awk 'FNR==NR{T[$1]=$0; gsub($1", *|, *$","",T[$1]); next} {print $0 " - " T[$1]}' FS=, ldap_jcon.txt FS=" " over90.txt

Code:
UNIQUENAME 2013-12-06 11:23:48.1 - UNIQUENAME
UNIQUENAME2 2014-03-10 12:22:29.91 - UNIQUENAME2
UNIQUENAME3-REED 2013-04-02 10:41:22.1 - UNIQUENAME3
UNIQUENAME4 2014-07-07 10:43:57.953 - UNIQUENAME4
UNIQUENAME5 2014-06-26 15:50:35.161 - UNIQUENAME5
UNIQUENAME6 2014-06-24 12:39:35.908 - UNIQUENAME6


Last edited by vbe; 11-05-2014 at 09:51 AM.. Reason: code tags not data
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. Shell Programming and Scripting

Join Two Files, Each With Different Delimiters

Hello All, I am trying to join two files, each with differing delimiters. file1 has doe, john 10.00 60.00 4.00 5.00 doe, jane 10.00 60.00 4.00 5.00 ...while file2 has the following content: doe, johnz 10.00::60.00:4.00:5.00 doe, janet 10.00:60.00::4.00:5.00 Currently, I have join... (2 Replies)
Discussion started by: techieg
2 Replies

3. Shell Programming and Scripting

Comparing files in a directory against an array of files

I hope I can explain this correctly. I am using Bash-4.2 for my shell. I have a group of file names held in an array. I want to compare the names in this array against the names of files currently present in a directory. If the file does not exist in the directory, that is not a problem.... (5 Replies)
Discussion started by: BudMan
5 Replies

4. Shell Programming and Scripting

How to put delimiters in text files after fix characters?

Hi , i have a text file in which i want to put delimiters after certain characters ( fix),. like put a delimiter (any like ,) after 1-3 character than 4 than 5 than 6-17 ..... files looks like this (original)... (8 Replies)
Discussion started by: anamdev
8 Replies

5. Shell Programming and Scripting

Comparing two files

I am trying to do a comparison between two files, and trying to output the difference between the two files. Let's take FileA.txt and FileB.txt for example: FileA.txt -------- Just A Fool:Christina Aguilera feat. Blake Shelton:Lotus (Deluxe Edition) Figure 8:Ellie Goulding:Halcyon... (4 Replies)
Discussion started by: todaealas
4 Replies

6. UNIX for Advanced & Expert Users

How to find duplicates contents in a files by comparing other files?

Hi Guys , we have one directory ...in that directory all files will be set on each day.. files must have header ,contents ,footer.. i wants to compare the header,contents,footer ..if its same means display an error message as 'files contents same' (7 Replies)
Discussion started by: Venkatesh1
7 Replies

7. Shell Programming and Scripting

Comparing the matches in two files using awk when both files have their own field separators

I've two files with data like below: file1.txt: AAA,Apples,123 BBB,Bananas,124 CCC,Carrot,125 file2.txt: Store1|AAA|123|11 Store2|BBB|124|23 Store3|CCC|125|57 Store4|DDD|126|38 So,the field separator in file1.txt is a comma and in file2.txt,it is | Now,the output should be... (2 Replies)
Discussion started by: asyed
2 Replies

8. UNIX for Dummies Questions & Answers

comparing two files having different delimiters using awk

hi, i have a file called file1.txt and it's contents are as below: file1.txt: ------- abc,123, thomas dab,234,muller gab,456,ram The lookup file's contents are as below: lookup.txt ---------- abc|japan dcd|US dab|china gab|brazil (3 Replies)
Discussion started by: amar1003
3 Replies

9. Shell Programming and Scripting

Need help comparing two files and deleting some things in those files!

So I have two files: File1 pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2 pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2 ref4948 1.1 treehouse.txt 1.6 ref8573 1.5 ref3284 1.4 ref5838... (24 Replies)
Discussion started by: linuxkid
24 Replies

10. UNIX for Advanced & Expert Users

comparing shadow files with real files

Hi I need to compare shadow file sizes with their real file counterparts. If the shadow file size differs form the realfile size then it must send a mail. My problem is that our system has over 1600 shadowfiles in different directories, with different names. the only consistancy is the .sh file... (4 Replies)
Discussion started by: terrym
4 Replies
Login or Register to Ask a Question