LinuxQuestions.org
Visit Jeremy's Blog.
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 04-26-2018, 06:38 PM   #1
starrysky1
LQ Newbie
 
Registered: Dec 2017
Posts: 26

Rep: Reputation: Disabled
BASH: Search a string and display the exact (from every character) number of times a substring happens inside it


I've searched all over and still cant find this simple answer. I'm sure its so easy. Please help if you know how to accomplish this.

sample.txt is:

AAAAA

I want to find the exact times the combination "AAA" happens. If you just use for example

grep -o 'AAA' sample.txt | wc -l

We receive a 1. This is the same as just searching the number of times AAA happens from with a standard text editor search box type search. However, I want the complete number of matches exactly, starting from each individual character which is exactly 3. We get this when we search from each character individually instead of treating each AAA hit like a box type block.

I am looking for the most squeezed in/most possibilities/literal exact number of occurences starting from every individual character of "AAA" in sample.txt, not just blocks of every time it finds it like it does in a normal text editor type search from the search box.

How do we accomplish this, preferrably in AWK?

THANKS A ZILLION !!!!
 
Old 04-26-2018, 07:04 PM   #2
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
Please be more specific.

If your sample text is AAAAbbcccAAAdddAAAAAeee
and your search string is AAA
how many "hits" do you expect? 3? 4? 6?

Daniel B. Martin
 
Old 04-26-2018, 07:53 PM   #3
starrysky1
LQ Newbie
 
Registered: Dec 2017
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by danielbmartin View Post
Please be more specific.

If your sample text is AAAAbbcccAAAdddAAAAAeee
and your search string is AAA
how many "hits" do you expect? 3? 4? 6?

Daniel B. Martin
6! Thanks a ton, I got to a solution by using the following script.

The working solution is:

Code:
#!/bin/bash

search_term="AAA"
text=$(cat sample.txt)
term_len=${#search_term}
occurences=0

# While the text is greater than or equal to the search term length
while [ "${#text}" -ge "$term_len" ]; do

    # Look at just the length of the search term
    text_substr=${text:0:${term_len}}

    # If we see the search term, increment occurences
    if [ "$text_substr" = "$search_term" ]; then
        ((occurences++))
    fi

    # Remove the first character from the main text
    # (e.g. "AAAAA" becomes "AAAA")
    text=${text:1}
done

printf "$occurences" "$search_term"

Last edited by starrysky1; 04-26-2018 at 07:56 PM.
 
1 members found this post helpful.
Old 04-26-2018, 09:21 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
Code:
search_term="AAA"
text=$(cat $Sample)
term_len=${#search_term}
text_len=${#text}
occurences=0
j=-1
# While the text is greater than or equal to the search term length
while [ "$j" -le "$text_len" ]; do
    let j=j+1
    # Look at just the length of the search term
    text_substr=${text:$j:${term_len}}

    # If we see the search term, increment occurences
    if [ "$text_substr" = "$search_term" ]; then
        ((occurences++))
    fi
done
printf "$occurences" "$search_term"
Daniel B. Martin

.
 
  


Reply

Tags
awk, bash, sed



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
bash: test for substring inside string ali_bush Linux - General 3 04-26-2018 07:31 PM
[SOLVED] Search exact string and get line number from where it is in text pedropt Programming 5 02-02-2018 10:02 AM
(SOLVED) Search and Replace Exact string without removing the line pedropt Programming 6 09-10-2017 03:53 AM
[Close] search for exact string on file and delete that line pedropt Programming 3 07-11-2017 03:36 PM
find exact string in text file and print it with few character before and behind mikemo39 Linux - General 5 10-20-2011 12:01 PM

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

All times are GMT -5. The time now is 12:00 PM.

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