×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Display how array elements are found in data file?

Display how array elements are found in data file?

Display how array elements are found in data file?

(OP)
Hello and thanks for reading my post, I really appreciate it.

Need a way to read from the arrayfile and search the datafile (with arrayfile elements) in a way that prints out how the arrayfile elements are found. An awk one liner is preferred.

arrayfile:

CODE --> bash

haha
hahh 
datafile:

CODE --> bash

aaahahahaaaahahahhaaahahahaaahahaaaa 
desiredoutputfile:

CODE --> bash

haha 1110111
hahh 0001000 
So its basically remove the last character from the arrayfile elements, search the datafile until you find the match, and if the next character after match is the same as was removed then write a 1 and if not then write a 0, It also needs to search in the most squeezed way possible (from beginning of each character in datafile, not by blocks like normal search in text editor, as portrayed in my previous post https://www.tek-tips.com/viewthread.cfm?qid=178648....

Thank you.

RE: Display how array elements are found in data file?

Got it by slightly modifying the script from previous post:

CODE

# Run:
#   awk -f starrysky1_02.awk datafile.txt arrayfile.txt

{ 
  # remove spaces
  gsub(/[ ]+/, "", $0)
} 

NR==FNR {
  data=$0;
  data_len = length(data)
  next
} 

{
  pattern = $0
  pattern_len = length(pattern)
  printf("%s\t", pattern)
  for (j=1; j+pattern_len-1 <= data_len; j++) {
    if (substr(data, j, pattern_len-1) ~ substr(pattern, 1, pattern_len-1)) {
      if (substr(data, j + pattern_len - 1, 1) ~ substr(pattern, pattern_len, 1)) {
        result = 1
      }
      else {
        result = 0 
      }
      printf("%s", result) 
    }      
  }
  print ""
} 

Output:

CODE

$ awk -f starrysky1_02.awk datafile.txt arrayfile.txt
haha	1110111
hahh	0001000 

Try to do from it an one liner.

RE: Display how array elements are found in data file?

(OP)
I am eternally grateful!!! Thank you soo much !!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close