Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display how array elements are found in data file? 1

Status
Not open for further replies.

starrysky1

Programmer
Apr 26, 2018
4
PR
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:
Bash:
haha
hahh
datafile:
Bash:
aaahahahaaaahahahhaaahahahaaahahaaaa
desiredoutputfile:
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
Thank you.
 
Got it by slightly modifying the script from previous post:

Code:
[COLOR=#0000ff]# Run:[/color]
[COLOR=#0000ff]#   awk -f starrysky1_02.awk datafile.txt arrayfile.txt[/color]

{ 
  [COLOR=#0000ff]# remove spaces[/color]
  [COLOR=#008b8b]gsub[/color]([COLOR=#ff00ff]/[[/color][COLOR=#6a5acd] [/color][COLOR=#ff00ff]][/color][COLOR=#6a5acd]+[/color][COLOR=#ff00ff]/[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]""[/color][COLOR=#6a5acd],[/color] [COLOR=#6a5acd]$0[/color])
} 

[COLOR=#6a5acd]NR[/color]==[COLOR=#6a5acd]FNR[/color] {
  data=[COLOR=#6a5acd]$0[/color][COLOR=#6a5acd];[/color]
  data_len = [COLOR=#008b8b]length[/color](data)
  [COLOR=#a52a2a][b]next[/b][/color]
} 

{
  pattern = [COLOR=#6a5acd]$0[/color]
  pattern_len = [COLOR=#008b8b]length[/color](pattern)
  [COLOR=#a52a2a][b]printf[/b][/color]([COLOR=#ff00ff]"[/color][COLOR=#6a5acd]%s[/color][COLOR=#6a5acd]\t[/color][COLOR=#ff00ff]"[/color][COLOR=#6a5acd],[/color] pattern)
  [COLOR=#a52a2a][b]for[/b][/color] (j=[COLOR=#ff00ff]1[/color][COLOR=#6a5acd];[/color] j+pattern_len[COLOR=#ff00ff]-1[/color] <= data_len[COLOR=#6a5acd];[/color] j++) {
    [COLOR=#a52a2a][b]if[/b][/color] ([COLOR=#008b8b]substr[/color](data[COLOR=#6a5acd],[/color] j[COLOR=#6a5acd],[/color] pattern_len[COLOR=#ff00ff]-1[/color]) ~ [COLOR=#008b8b]substr[/color](pattern[COLOR=#6a5acd],[/color] [COLOR=#ff00ff]1[/color][COLOR=#6a5acd],[/color] pattern_len[COLOR=#ff00ff]-1[/color])) {
      [COLOR=#a52a2a][b]if[/b][/color] ([COLOR=#008b8b]substr[/color](data[COLOR=#6a5acd],[/color] j + pattern_len - [COLOR=#ff00ff]1[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]1[/color]) ~ [COLOR=#008b8b]substr[/color](pattern[COLOR=#6a5acd],[/color] pattern_len[COLOR=#6a5acd],[/color] [COLOR=#ff00ff]1[/color])) {
        result = [COLOR=#ff00ff]1[/color]
      }
      [COLOR=#a52a2a][b]else[/b][/color] {
        result = [COLOR=#ff00ff]0[/color] 
      }
      [COLOR=#a52a2a][b]printf[/b][/color]([COLOR=#ff00ff]"[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff]"[/color][COLOR=#6a5acd],[/color] result) 
    }      
  }
  [COLOR=#a52a2a][b]print[/b][/color] [COLOR=#ff00ff]""[/color]
}

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

Try to do from it an one liner.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top