compare strings, words in different order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare strings, words in different order
# 1  
Old 03-20-2009
compare strings, words in different order

Hi,

Would anyone know how to compare two strings, and only throw an error if there were different words, not that the same words were in a different order?

e.g
"A B C" vs "B C A" ->OK
"A B C" vs "A D C" -> BAD

Thanks!
# 2  
Old 03-20-2009
Hammer & Screwdriver the start of a process

three files (your inputs); converted so each character is on its own line
Code:
> cat file1
A B C
> cat file1 | tr " " "\n" >file1a
> cat file2 
B C A
> cat file2 | tr " " "\n" >file2a
> cat file3
A D C
> cat file3 | tr " " "\n" >file3a

I egrep the 2nd file against the first, and the reult is three items
Code:
> cat file1a | egrep -f file2a
A
B
C

I egrep the 3rd file against the first, but the result is two items
Code:
> cat file1a | egrep -f file3a
A
C

So, assigning the number of lines (wc -l) to a variable, you can compare the number at the end of each process. This (command codes) can be simplified in areas, but I did it this way to help illustrate the process for you.
# 3  
Old 03-20-2009
With awk (the code assumes interactive session, you should modify it if you want to use in a non interactive script):

Code:
awk 'BEGIN {
  n = split("A B C", t)
  for (i=1; i<=n; i++) _[t[i]]
  }
{
  for (i=1; i<=NF; i++)
    if (!($i in _)) {
      print "bad"
      next
    } 
  print "ok"
}'

For example:
Code:
zsh-4.3.9% awk 'BEGIN {
  n = split("A B C", t)
  for (i=1; i<=n; i++) _[t[i]]
  }
{
  for (i=1; i<=NF; i++)
    if (!($i in _)) {
      print "bad"
      next
    }
  print "ok"
}'
B C A
ok
A D C
bad

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and repllace of strings with space between words

Dear all, I have gone through all the search and replace requests but none of them meet my particular need. I have a huge file in which all Unicode characters are stored as Names. A sample is given below. I want to replace strings in that file with a mapper from another file termed as master.dic. ... (4 Replies)
Discussion started by: gimley
4 Replies

2. UNIX for Dummies Questions & Answers

Joining ends of strings in certain order with repeated ID's

I posted this a few days ago and got some help (Putting together substrings if pattern is matched - Page 2 | Unix Linux Forums | Shell Programming and Scripting) But I am now stuck on an issue that is similar but not the same really. I want to join parts of one line with parts of another line... (8 Replies)
Discussion started by: verse123
8 Replies

3. UNIX for Dummies Questions & Answers

Strings in ascending order

Hi, I have a sequence which has 30000 strings which looks like this >string2991 234445 >string224 470561 >string121 675386 >string4098 177229 >string8049 255838 >string8 672382 >string1115 578415 I want it to be arranged in ascending order >string8 672382 >string121... (5 Replies)
Discussion started by: siya@
5 Replies

4. UNIX for Dummies Questions & Answers

Search file or log for words or strings

i want to search a log for occurrences of words and i want the result to tell me how many lines in the log contained each word. if i type a command like this: egrep "cat|dog|monkey|bananas|bike" logfile i would like a response like this: cat=3,dog=17,monkey=1,bananas=102,bike=51 the... (12 Replies)
Discussion started by: SkySmart
12 Replies

5. Shell Programming and Scripting

Sorting strings in reverse order

Hello, I have a large database of words and would like them sorted in reverse order i.e. from the end up. An example will make this clear: I have tried to write a program in Perl which basically takes the string from the end and tries to sort from that end but it does not seem... (5 Replies)
Discussion started by: gimley
5 Replies

6. UNIX for Dummies Questions & Answers

Searching for multiple words on a line in any order issue

Hi again I have figured out how to be able to sort through lines in a file with multiple words in any order and display them using this command: cat file | grep -i $OPTION1 | grep -i $OPTION2 | grep -i $OPTION3 OPTION1 is 2008, OPTION2 is Mar, OPTION 3 is Tue Result: Tue Mar 25... (4 Replies)
Discussion started by: semaj
4 Replies

7. Programming

Sorting a vector of strings into numerical order.

I have a vector of strings that contain a list of channels like this: 101,99,22HD,432,300HD I have tried using the sort routine like this: sort(mychans.begin(),mychans.end()); For some reason my channels are not being sorted at all. I was hoping someone might have some input that might... (2 Replies)
Discussion started by: sepoto
2 Replies

8. Shell Programming and Scripting

awk script to get words which are not in a particula order

Hi all, I need a big help. I've a requirment which should be done in awk scripting only.. My requirment is: In below file, there are some lines started with 'master replica:'. i want to get the exact word in that line which should be before '@', and i've to create a file with these names... (5 Replies)
Discussion started by: raghu.iv85
5 Replies

9. Shell Programming and Scripting

Keep lines with specific words up in an order

I hava a file with following data: number|CREDIT_ID|NULL date|SYS_CREATION_DATE|NULL varchar2|GGS_COMMIT_CHAR|NULL varchar2|GGS_OP_TYPE|NULL number|GGS_SCN|NULL| number|GGS_LOG_SEQ|NULL number|GGS_LOG_POS|NULL number|GGS_ORACREC_SCN|NULL varchar2|BATCH_ID|NULL char|GGS_IMAGE_TYPE|NULL ... (6 Replies)
Discussion started by: kolesunil
6 Replies

10. Shell Programming and Scripting

shell program for sorting strings in an alphabetical order

Hi, I trying to find the solution for writing the programming in unix by shell programming for sorting thr string in alphabetical order. I getting diffculty in that ,, so i want to find out the solution for that Please do needful Thanks Bhagyesh (1 Reply)
Discussion started by: bp_vanarse
1 Replies
Login or Register to Ask a Question