Compare and print out data only appear in file 1 problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare and print out data only appear in file 1 problem
# 1  
Old 09-27-2010
Compare and print out data only appear in file 1 problem

Below is the data content of file_1 and file_2:
file_1
Code:
>sample_1
FKGJGPOPOPOQA
ASDADWEEWERE
ASDAWEWQWRW
ASDASDASDASDD

file_2
Code:
>sample_1
DRTOWPFPOPOQA
ASDADWEEASDF
ASDADRTYWRW
ASDASDASDASDD

I got try the following perl script. Unfortunately, it can't give my desired output result Smilie
Code:
cat file_1
>sample_1
FKGJGPOPOPOQA
ASDADWEEWERE
ASDAWEWQWRW
ASDASDASDASDD

cat file_2
>sample_1
DRTOWPFPOPOQA
ASDADWEEASDF
ASDADRTYWRW
ASDASDASDASDD

perl -e ' ($file1, $file2) = @ARGV; $printed = 0; open F2, $file2; while (<F2>) { $h2{$_}++ }; $count2 = $.; open F1, $file1; while (<F1>) { if (! $h2{$_}) { print $_; $printed++; } } $count1 = $.; warn "\nRead $count1 lines from $file1 and $count2 lines from $file2.\nPrinted $printed lines found in $file1 but not in $file2\n\n" ' file_1 file_2 > file_3

cat file_3
FKGJGPOPOPOQA
ASDADWEEWERE
ASDAWEWQWRW

Desired output file content:
Code:
FKGJGPO
WERE
WEWQ

The perl command that I used print out the whole content instead of just print out the specific special content that I interested in file_1Smilie
Thanks a lot for any advice.
# 2  
Old 09-27-2010
Code:
$ awk -F "" '
NR==FNR{a[NR]=$0;next} 
!/>/ { 
          split(a[FNR],b,""); 
          {     for (i=1;i<=NF;i++) {if ($i!=b[i]) printf b[i]}
                printf RS
          }
      } 
' file1 file2

FKGJGO
WERE
WEWQ

This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 09-27-2010
Thanks again for your help, rdcwayx Smilie
Do you got any idea regarding my another question facing at the following link?
https://www.unix.com/shell-programmin...convert-2.html
Thanks first ^^
# 4  
Old 09-28-2010
Hi rdcwayx,
Do you got any idea to archive the below goal?
file_1
Code:
>sample_1
FKGJGPOPOPOQA
ASDADWEEWERE
ASDAWEWQWRW
ASDASDASDASDD

file_2
Code:
>sample_1
ASDFRPOPOPPWE
ASDADWEERTTY
ASDAPERTWRW
ASDASDASDASDD

Desired output:
Code:
FKGJG     OQA
        WERE
    WEWQ

My purpose just replaced those similar content with either "empty" or "tab" delimiter in order to separate those special content in every line.
eg.
I prefer the result look like this
Code:
FKGJG     OQA

Instead of
Code:
FKGJGOQA

Thanks again and a lot for your advice all the times, rdcwayx
# 5  
Old 09-28-2010
Code:
awk -F "" '
NR==FNR{a[NR]=$0;next}
!/>/ {
          split(a[FNR],b,"");
          {     for (i=1;i<=NF;i++) {printf ($i!=b[i])?b[i]:" "}
                printf RS
          }
      }
' file1 file2

FKGJG     OQA
        WERE
    W WQ

This User Gave Thanks to rdcwayx For This Post:
# 6  
Old 09-28-2010
Quote:
$ awk -F "" '
NR==FNR{a[NR]=$0;next}
!/>/ {
split(a[FNR],b,"");
{ for (i=1;i<=NF;i++) {if ($i!=b[i]) printf b[i]}
printf RS
}
}
' file1 file2
Could you please explain the code..
# 7  
Old 09-28-2010
Hi rdcwayx,
Really thanks for your help in awk Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare & print content of file which is not matching

Hi All I want to compare 2 files using awk and get output of content which is not matching I have 2 files a.txt 123 456 780 143 b.txt A|B|C|167|D|E C|K|D|123|D|E A|B|D|789|G|F C|D|G|143|A|B Not matching line from b.txt O/P A|B|C|167|D|E A|B|D|789|G|F (3 Replies)
Discussion started by: aaysa123
3 Replies

2. Shell Programming and Scripting

Problem facing to compare different column and print out record with smallest number

Hi, Input file 1 : 37170 37196 77 51 37174 37195 73 52 37174 37194 73 53 Desired Output file 1 : 37170 37196 77 51 Input file 2 : 37174 37195 73 0 37170 37196 77 0 Desired Output file 2 : 37174 37195 73 0 (1 Reply)
Discussion started by: cpp_beginner
1 Replies

3. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

4. Shell Programming and Scripting

Compare a file with all others then print off data

my script so far nawk -F, 'NR==FNR{a++;next} a{b++} END{for(i in b){if(b-1){print i";\t\t"b}else{print "NEW:"i";\t\t1"} } }' OFS=, 20111228.csv *.csv | sort NE:221478,SHELF:13,SLOT:4; 2 NE:221726,SHELF:8,SLOT:1; 2 NE:222318,SHELF:14,SLOT:1; 9... (20 Replies)
Discussion started by: llcooljatt
20 Replies

5. UNIX for Dummies Questions & Answers

Compare 2 files print the lines of file 2 that contain a string from file 1

Hello I am a new unix user, and I have a work related task to compare 2 files and print all of the lines in file 2 that contain a string from file 1 Note: the fields are in different columns in the files. I suspect the is a good use for awk? Thanks for your time & help File 1 123 232 W343... (6 Replies)
Discussion started by: KevinRidley
6 Replies

6. Shell Programming and Scripting

Compare two file and print same line

i want to compare two file and print same line file1 12345 a 23456 a 45678 a 45679 a file2 23456 a 34567 a 45679 a output 23456 a 45679 a any one can help me? Thank you (7 Replies)
Discussion started by: bleach8578
7 Replies

7. Shell Programming and Scripting

compare two columns of different files and print the matching second file..

Hi, I have two tab separated files; file1: S.No ddi fi cu o/l t+ t- 1 0.5 0.6 o 0.1 0.2 2 0.2 0.3 l 0.3 0.4 3 0.5 0.8 l 0.1 0.6 ... (5 Replies)
Discussion started by: vasanth.vadalur
5 Replies

8. Shell Programming and Scripting

Compare selected columns from a file and print difference

I have learned file comparison from my previous post here. Then, it is comparing the whole line. Now, i have a new problem. I have two files with 3 columns separated with a "|". What i want to do is to compare the second and third column of file 1, and the second and third column of file 2. And... (4 Replies)
Discussion started by: kingpeejay
4 Replies

9. Shell Programming and Scripting

compare 2 file and print difference in the third file URG PLS

Hi I have two files in unix. I need to compare two files and print the differed lines in other file Eg file1 1111 2222 3333 file2 1111 2222 3333 4444 5555 newfile 4444 5555 Thanks In advance (3 Replies)
Discussion started by: evvander
3 Replies

10. UNIX for Dummies Questions & Answers

Compare Data in the same file

Dear Unix-Gurus, I'm trying to write a script to compare the data in a log file. Here's how my logfile will look like: 'List All A0 Data in Destination Server' A0567 A0678 A0789 List A0 Files in Source Server A0567 A0678 A0789 So if the file match in Source Server match Destination... (1 Reply)
Discussion started by: lweegp
1 Replies
Login or Register to Ask a Question