LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2018, 01:55 PM   #1
starrysky1
LQ Newbie
 
Registered: Dec 2017
Posts: 26

Rep: Reputation: Disabled
AWK: How to display how array elements are found in data file?


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

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

arrayfile:
Code:
haha
hahh
datafile:
Code:
aaahahahaaaahahahhaaahahahaaahahaaaa
desiredoutputfile:
Code:
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.linuxquestions.org/quest...98#post5847798).

Thank you infinitely.

Last edited by starrysky1; 05-02-2018 at 01:57 PM.
 
Old 05-02-2018, 02:39 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,776

Rep: Reputation: 7996Reputation: 7996Reputation: 7996Reputation: 7996Reputation: 7996Reputation: 7996Reputation: 7996Reputation: 7996Reputation: 7996Reputation: 7996Reputation: 7996
Quote:
Originally Posted by starrysky1 View Post
Hello and thanks for reading my post, I really appreciate it. Need a way to read from the arrayfile and search the datafile in a way that prints out how the arrayfile elements are found. An awk one liner is preferred.
arrayfile:
Code:
haha
hahh
datafile:
Code:
aaahahahaaaahahahhaaahahahaaahahaaaa
desiredoutputfile:
Code:
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.linuxquestions.org/quest...98#post5847798).
Read the "Question Guidelines" link in my posting signature. We're happy to help, but just telling us what you want and not showing your own efforts to solve your own problem, isn't a good thing. This is something you've done in many previous threads as well.

Post what you have written/done/tried so far.
 
1 members found this post helpful.
Old 05-02-2018, 11:48 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Not only do I agree 100% with TB0ne, but how is this any different than the linked previous question? What attempts to alter the suggested previous solution have you made to allow for whatever seems
to be your new criteria?
 
1 members found this post helpful.
Old 05-03-2018, 12:55 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
To echo grail and TB0ne, if you show an earnest effort we will help. When you comply I will post a tested-and-working solution here.

Daniel B. Martin

.
 
2 members found this post helpful.
Old 05-04-2018, 07:14 PM   #5
starrysky1
LQ Newbie
 
Registered: Dec 2017
Posts: 26

Original Poster
Rep: Reputation: Disabled
Finally!!! I got it down! Solution is:
Code:
{ 
  # 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 ""
}

Last edited by starrysky1; 05-04-2018 at 07:17 PM.
 
Old 05-05-2018, 11:25 AM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile1 ...
Code:
aaahahahaaaahahahhaaahahahaaahahaaaa
... and this InFile2 ...
Code:
haha
hahh
... this awk ...
Code:
# df = datafile
# sc = short candidate
# OL = Output Line
echo; echo "Method #1 of LQ Member danielbmartin."
awk '{if (NR==FNR) df=$0
      else {OL=""; sc=substr($0,1,length($0)-1)
            for (k=1;k<=length(df);k++)
              {if ($0==substr(df,k,length($0))) OL=OL"1"
          else if (sc==substr(df,k,length(sc))) OL=OL"0"} 
      print $0,OL}}'  $InFile1 $InFile2 >$OutFile
... produced this OutFile ...
Code:
haha 1110111
hahh 0001000
Daniel B. Martin

.
 
Old 05-05-2018, 05:57 PM   #7
starrysky1
LQ Newbie
 
Registered: Dec 2017
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by danielbmartin View Post
With this InFile1 ...
Code:
aaahahahaaaahahahhaaahahahaaahahaaaa
... and this InFile2 ...
Code:
haha
hahh
... this awk ...
Code:
# df = datafile
# sc = short candidate
# OL = Output Line
echo; echo "Method #1 of LQ Member danielbmartin."
awk '{if (NR==FNR) df=$0
      else {OL=""; sc=substr($0,1,length($0)-1)
            for (k=1;k<=length(df);k++)
              {if ($0==substr(df,k,length($0))) OL=OL"1"
          else if (sc==substr(df,k,length(sc))) OL=OL"0"} 
      print $0,OL}}'  $InFile1 $InFile2 >$OutFile
... produced this OutFile ...
Code:
haha 1110111
hahh 0001000
Daniel B. Martin

.
Awesome!! Extremely grateful!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed for using awk to parse a file to make array for bash script tallmtt Programming 12 04-14-2012 01:16 PM
need help with writing array elements into a file jayran Linux - Newbie 22 12-16-2009 07:21 AM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM
awk: Using split to divide string to array. How do I find out the number of elements? vxc69 Programming 9 02-09-2008 12:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration