character-by-character comparison of strings


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers character-by-character comparison of strings
# 1  
Old 07-26-2010
character-by-character comparison of strings

This might be a dummy question, but is there a command in UNIX that compare two strings character-by-character and display the difference?

---------- Post updated at 11:25 AM ---------- Previous update was at 10:32 AM ----------

Or probably what I'm looking is how to break a string into array, such as below for example:

Code:
STRING="Hello"
STRING[0]='H'
STRING[1]='e'
STRING[2]='l'
STRING[3]='l'
STRING[4]='o'


Last edited by Scott; 07-26-2010 at 03:02 AM.. Reason: Code tags
# 2  
Old 07-26-2010
Code:
#!/bin/bash
for((i=0;i<${#STRING};i++)); do echo ${STRING:$i:1}; done

# 3  
Old 07-26-2010
Code:
# i=0;for char in `echo Hello | fold -w 1 ` ; do
STRING[i]=$char
((i++))
done

# 4  
Old 07-26-2010
Code:
IFS=$'\n'
var=$(echo "hello" | fold -w 1)
set -- $var
echo $1
echo $2
echo $3

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove certain character strings with awk?

Hi all, I need to remove DBPATH= and /db from the string below using awk (or sed, as it also exists on the machine). Input: DBPATH=/some/path/database/db Desired output: /some/path/database Thank you! (8 Replies)
Discussion started by: ejianu
8 Replies

2. UNIX for Dummies Questions & Answers

Extracting 22-character strings from text using sed/awk?

Here is my task, I feel sure this can be accomplished with see/awk but can't seem to figure out how. I have large flat file from which I need to extract every case of a pairing of characters (GG) in this case PLUS the previous 20 characters. The output should be a list (which I plan to make... (17 Replies)
Discussion started by: Twinklefingers
17 Replies

3. Shell Programming and Scripting

Selecting strings with - as first character in bash

I have a variable containing a list of strings, and want to create a string with the first character being -. Example: var="-name fred paul -surname winnett dimech" I want a string, namely errStr="-name -surname" (2 Replies)
Discussion started by: kristinu
2 Replies

4. Shell Programming and Scripting

replace two character strings by two variables with sed command

Hello, I want to writte a script that replace two character strings by two variables with the command sed butmy solution doesn't work. I'm written this: sed "s/TTFactivevent/$TTFav/g && s/switchSLL/$SLL/g" templatefile. I want to replace TTFactivevent by the variable $TTFav, that is a... (4 Replies)
Discussion started by: POPO10
4 Replies

5. Shell Programming and Scripting

Uppercase/lowercase comparison of one character per line with awk??

Another frustrating scripting problem from a biologist trying to manipulate a file with several millions line. For each of the line I need to compare the uppercase A or C or G or T with the lowercase a or c or g or t. If there are more uppercases, a + should be added to a new column, otherwise a -... (10 Replies)
Discussion started by: ivpz
10 Replies

6. UNIX for Dummies Questions & Answers

Getting strings before and after a character

OK This one has me stumped. I have the following line, program name - the program description that can also contain a hyphen - character. I'm need to separate the "program name" from the program description. I've tried using an array function with the - as delimiter, but I ran into a... (2 Replies)
Discussion started by: ricksj
2 Replies

7. UNIX for Dummies Questions & Answers

Help to replace character strings

Hello Can Any1 help me. I want to replace a specific character string inside a file at a specific location with a particular character with the help of a command or a shell script. The tr command replaces a specific character with another for all the occurences of that character in the file. I... (5 Replies)
Discussion started by: rahulrathod
5 Replies

8. Shell Programming and Scripting

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 (5 Replies)
Discussion started by: etherite
5 Replies

9. Shell Programming and Scripting

Perl RegExp to remove last character from strings

I use SAS (a statistical software) and have to remove last character or the last 1/2 numbers that appear after characters from the string using Perl Regular Expression (which is recognized by SAS). Input: f183ii10 f183ii2 f182ii1 f182ii2 f183iim f22ii f22ii11 f22ii12 pmh4 pmhm Desired... (2 Replies)
Discussion started by: ospreyeagle
2 Replies

10. UNIX for Dummies Questions & Answers

character comparison problem

I have three lines in a log that I want to check to make sure that they say the following (mm/dd/yyyy and hh:mm:ss changes depending in when the log is generated): '*** Testing file for mm/dd/yyyy' '*** End of procedure! Time was: hh:mm:ss' '*** End ***' I did some sed... (13 Replies)
Discussion started by: giannicello
13 Replies
Login or Register to Ask a Question