Help with script or command to differentiate difference between two input file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with script or command to differentiate difference between two input file?
# 1  
Old 12-26-2010
Help with script or command to differentiate difference between two input file?

I got two file write now.
Input file 1:
Code:
>data_1
DSFDFDSGFDSGSGEGTRTRERPOYIORPGKKGDSPKFSDKFPSDKFSPFS
>data_34
WEEREREWREWOIQOPIEPDSKLFNDSFNSKNCASKJHDAFHAOUDFEOWWIOUFEWIUEWIRUEWIRUEWIORUEWOREWR
>data_21
ASDASDQWEQWRQERFWPOTGIUWEIPOFIOFDSNFKSJDNFSKDHFKDSJHFKDSJHF
>data_4
SDFDSFREREWPTIOEPOTIERWPOI
.
.

Input file 2:
Code:
>data_1
DSFDFDXXXXXXSGEGTRTRERPOYIORPGKKGDSPKFSDXXXXXXXXXXS
>data_34
WEEREREWREWOIQOPIEPDSKLFXXXXXXKNCASKJHDAFHAOUDFEOWWIOUFEWIUEWIRUEWIRUEWIORUEWOREWR
>data_21
ASDASDQWXXXXXXXXXXOTGIUWEIPOFIOFDSNFKSJDNFSKDHFKDSJHFKDSJHF
>data_4
SDFDSFREREWPTIOEPOTIEXXXXX
.
.

Desired output
Code:
      SGFDSG                            KFPSDKFSPFS

                        NDSFNS

        EQWRQERFWP

                    RWPOI
.
.

The word in BOLD is the difference part between input file 1 and input file 2.
I would like to export the word that only shown in input file 1 but not shown in input file 2 into another output file.
Thanks.
# 2  
Old 12-26-2010
do you want the letters with XXXXXX in second file matched words from First file ..

is that is what you want ??
# 3  
Old 12-26-2010
Hi expert, I want to print out the data that XXXXXX in file 2 originally represent what in file 1.
eg.
Input file 1
Code:
>data_1
DSFDFDSGFDSGSGEGTRTRERPOYIORPGKKGDSPKFSDKFPSDKFSPFS

Input file 2
Code:
>data_1
DSFDFDXXXXXXSGEGTRTRERPOYIORPGKKGDSPKFSDXXXXXXXXXXS

By comparing both file, XXXXXX and XXXXXXXXXXX in input file 2 is originally represent
"SGFDSG" and "KFPSDKFSPFS" in input file 1
"SGFDSG" and "KFPSDKFSPFS" is the data that I plan to appear in desired output file.
Thanks.
# 4  
Old 12-26-2010
More a discussion post than a serious attempt at a commercial solution. Here is a chronically inefficient method which works for small files.
Assumes that there are no "tab" or "space" characters in either input file because we use "tab" to separate the results of the "paste" statement (which just placed the corresponding lines from file1.txt and file2.txt side-by-side).

Code:
#!/bin/ksh
paste file1.txt file.txt | awk '{print $1,$2}' | while read line1 line2
do
        if [ "${line1}" = "${line2}" ]
        then
                printf "\n"             # Output blank line
                continue
        fi
        #
        counter=0
        echo "${line1}"|fold -w 1|while read char1
        do
                counter=`expr ${counter} + 1`
                char2=`echo "${line2}"|cut -c${counter}`
                if [ "${char1}" = "${char2}" ]
                then
                        printf " "      # Single space
                else
                        printf "${char1}"
                fi
        done
        printf "\n"     # Newline
done


./scriptname

      SGFDSG                            KFPSDKFSPF 

                        NDSFNS                                                  

        EQWRQERFWP                                         

                     RWPOI


Hmm. I got "KFPSDKFSPF" not "KFPSDKFSPFS".

Last edited by methyl; 12-26-2010 at 08:20 PM.. Reason: Excess blank lines.
This User Gave Thanks to methyl For This Post:
# 5  
Old 12-26-2010
Hi.

In whatever language one wishes: read a character from each file (or read lines, then step through the characters). Apply the following logic:
Code:
file1 file2 matchX? result
a     b     no      space->output
c     X     yes     c->output

@methyl:
I got the same off-by-one result. I think the picture template of Xs from the OP for that line is wrong ... cheers, drl
# 6  
Old 12-31-2010
Hi, expert.
You are right.
I would like to find out what is the XXXXXX in file 2 based on the data source of file 1.
Do you have any idea to solve the problem?
Thanks first.

---------- Post updated at 12:12 AM ---------- Previous update was at 12:10 AM ----------

I just try using the awk command. It seems like taken long time when my input file 1 and input file 2 is a very huge file, eg. 1GB
Any perl language expert got better idea or solutions?
# 7  
Old 12-31-2010
Code:
awk '{n=split($0,a,"");getline < "file2"; split($0,b,"");
      for (i=1;i<=n;i++) printf (a[i]==b[i])?" ":a[i];printf "\n"}' file1

This User Gave Thanks to rdcwayx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read input file with in awk script not through command line

Hi All, Do we know how to read input file within awk script and send output toanother log file. All this needs to be in awk script, not in command line. I am running this awk through crontab. Cat my.awk #!/bin/awk -f function test(var){ some code} { } END { print"test code" } (5 Replies)
Discussion started by: random_thoughts
5 Replies

2. Shell Programming and Scripting

Difference between command line and script

i have a bit of a unique question. is there a way to know if a script is being run from from the command line or from another script? example: command line: ### ./autorun.sh ERROR: not permitted to run script from the commandline. exiting... but if i put "autorun.sh" into another... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

how to differentiate file and directory name using ls command

how to differentiate file and directory name using ls command. l (3 Replies)
Discussion started by: jhon123
3 Replies

4. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

5. UNIX for Dummies Questions & Answers

Bash script to delete file input on command line

1) I wrote a script and gave the desired permissions using "chmod 755 scriptname". Now if i edit the script file, why do i need to set the permission again? Didn't i set the permission attribute.. or if i edit the file, does the inode number of file changes? 2) I am running my unix on a server... (1 Reply)
Discussion started by: animesharma
1 Replies

6. Shell Programming and Scripting

Need script to take input from file, match on it in file 2 and input data

All, I am trying to figure out a script to run in windows that will allow me to match on First column in file1 to 8th Column in File2 then Insert file1 column2 to file2 column4 then create a new file. File1: 12345 Sam 12346 Bob 12347 Bill File2:... (1 Reply)
Discussion started by: darkoth
1 Replies

7. UNIX for Dummies Questions & Answers

differentiate between a file and a device

sorry probably a beginner question but i was just wondering how unix does this as device are treated as files? (6 Replies)
Discussion started by: keith_hampson
6 Replies

8. Shell Programming and Scripting

Perl code to differentiate numeric and non-numeric input

Hi All, Is there any code in Perl which can differentiate between numeric and non-numeric input? (11 Replies)
Discussion started by: Raynon
11 Replies

9. UNIX for Dummies Questions & Answers

how to differentiate a file from a folder in a FIND?

I have to read a complete folder and if it is a file older that 7 days I have to copy it elsewhere and if it is a folder nothing to make. The way I do it: for I in `find /home/. -name "*" -mtime +7` do cp -Rf $I /home/elsewhere/. done Am I okay with the way I want to do it? Help... (3 Replies)
Discussion started by: denysQC
3 Replies

10. Shell Programming and Scripting

How to input username on text file into finger command on shell script

I'm trying to clean up my server and I have the list of some "special" users stored on the text file like this Now I want to write a shell script to finger all of them so I can have some kind of ideas who they are but here comes the problem....I completely forgot how to do it with shell... (3 Replies)
Discussion started by: Micz
3 Replies
Login or Register to Ask a Question