Finding character mismatch position in two strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding character mismatch position in two strings
# 1  
Old 07-15-2008
Finding character mismatch position in two strings

Hello,

I would like to find an efficient way to compare a pair of strings that differ at one position, and return the difference and position.

For example:

String1 123456789

String2 123454789

returning something - position 6, 6/4

Thanks in advance,

Mike
# 2  
Old 07-15-2008
Question have you looked at the diff command?

I think it has an option to process byte by byte; seems to be what you are looking for.
# 3  
Old 07-15-2008
or awk
Code:
echo 12345678 12345478 | \
awk ' BEGIN {pos=0}
    {
     max=(length($1) >= length($2))? length($1): length($2)     
     for(i=1; pos == 0 && i <= max; i++)
     {
     	 v1=substr($1, i, 1)  
     	 v2=substr($2, i, 1) 
     	 if(v1 != v2){ pos=i } 
     }     
    }
    END { if(pos) {printf("%d %d/%d\n", pos, v1, v2) }}'

# 4  
Old 07-15-2008
An awk solution is great! Thanks Jim.

I've also just found cmp in the GNU DiffUtilities package, but yours is pretty much what I was looking for.
# 5  
Old 07-21-2008
Oh bother!

It turns out that I didn't fully explain what I was trying to do. Jim's solution works for a single pair of strings that I wish to compare, however I actually have a file with pairs of strings on each line. I would like to carry out the comparison on each line in turn. Jim's awk script just checks the first line.

Sorry if I am being dumb about this.

Mike
# 6  
Old 07-21-2008
Here's a minor adaptation of jim's script. It prints the line number and the offset, or nothing if both tokens are identical.

Code:
awk '{ pos=0
     max=(length($1) >= length($2))? length($1): length($2)     
     for(i=1; pos == 0 && i <= max; i++)
     {
     	 v1=substr($1, i, 1)  
     	 v2=substr($2, i, 1) 
     	 if(v1 != v2) printf "%i: %d %d/%d\n", NR, pos, v1, v2
     }
    }' filename


Last edited by era; 07-21-2008 at 09:45 AM.. Reason: NR, not RN (duh)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print strings from a particular position in each line

I am using bash in Fedora 30 From the below lines (ls -l output), how can I print whatever is between the strings 'status_' and '.log' $ ls -l | grep -i status -rw-rw-r--. 1 sysadmin sysadmin 378530 Nov 11 21:58 status_vsbm1.log -rw-rw-r--. 1 sysadmin sysadmin 428776 Nov 11 21:58... (8 Replies)
Discussion started by: kraljic
8 Replies

2. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

3. Shell Programming and Scripting

Finding a certain character in a filename and count the characters up to the certain character

Hello, I do have folders containing having funny strings in their names and one space. First, I do remove the funny strings and replace the space by an underscore. find . -name '* *' | while read file; do target=`echo "$file" | sed 's/... (2 Replies)
Discussion started by: tempestas
2 Replies

4. Shell Programming and Scripting

Find character and Replace character for given position

Hi, i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command. thanks in advance, Sara (9 Replies)
Discussion started by: Sara183
9 Replies

5. Shell Programming and Scripting

Find position of character in multiple strings in a file

Greetings. I have a file with information like this: AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU? AMNDHRKEEU?AMNDHREOEU? AMNDHREU?AHRKEOEU?AMNDHRKEU?AMNDKEOEU? What I need to extract is the position, in every line, of every occurrence of '?' A desired output would be something... (6 Replies)
Discussion started by: Twinklefingers
6 Replies

6. Shell Programming and Scripting

Finding position of space in a variable

HI All, am trying to find the position of space in a variable, it is working for other characters other than space ulab="ulab1|ulab2" find_pos=`expr index $ulab '|'` echo $find_pos above code worked fine but below one says syntax error ulab="ulab ulab2" find_pos=`expr index $ulab ' '`... (2 Replies)
Discussion started by: ulab
2 Replies

7. UNIX for Dummies Questions & Answers

Help with finding matching position on strings

I have a DNA file like below and I am able to write a short program which finds/not an input motif, but I dont understand how I can include in the code to report which position the motif was found. Example I want to find the first or all "GAT" motifs and want the program to report which position... (12 Replies)
Discussion started by: pawannoel
12 Replies

8. Shell Programming and Scripting

Finding relative position in a file

Hi, I have a file like 123 aaaaaaaaa ddddddddd vvvvvvvvv 345 ssssssssssss dddddddddd fffffffffff dddd ff 567 --------- sssssssss ddddddd eeeeeeeee (4 Replies)
Discussion started by: saltysumi
4 Replies

9. Shell Programming and Scripting

Help in finding the max and min position

Hi, I have this input file called ttbitnres (which is catenated and sorted):- 8 0.4444 213 10 0.5555 342 11 0.5555 321 12 0.5555 231 13 0.4444 400 My code is at :- #!/bin/bash echo -e Version "\t" Number of Pass "\t" Number of Fail "\t" Rank Position "\t"Min "\t" Max... (1 Reply)
Discussion started by: ahjiefreak
1 Replies

10. Shell Programming and Scripting

How to insert strings at certain position

Hi, I need to insert strings "0000 00" at the each line within the file. The postion is 37 to 42. ex. name1 name2 0000 00 nam name 0000 00 The "0000 00" in two lines should be lined up. I don't know why it's not lined up when I posted it. Can anyone help? (14 Replies)
Discussion started by: whatisthis
14 Replies
Login or Register to Ask a Question