Saturday, March 18, 2023
HomeGolangFilter a textual content by the key phrase - Getting Assist

Filter a textual content by the key phrase – Getting Assist


Hello group,

I’m making an attempt to filter a textual content by the key phrase with golang. Mainly, what I’m executing:

bundle important

import (
	"fmt"
	"os/exec"
)

func branches() {
	out, err := exec.Command("git", "department", "-a", "--sort=-committerdate", "--column", "--format="%(committerdate)%09%(refname:brief)"").Output()

	if err != nil {
		// log.Deadly(err)
		fmt.Println("Could not discover any branches within the repository")
	}

	str1 := string(out)

	fmt.Println(str1)
}

func important() {
	fmt.Printf("1. CHECK ALL BRANCHES: ")
	branches()
}

And getting:

go run important.go
1. CHECK ALL BRANCHES: 'Mon Oct 3 12:20:53 2022 +0000	grasp'
'Mon Oct 3 12:20:53 2022 +0000	origin/HEAD'
'Mon Oct 3 12:20:53 2022 +0000	origin/grasp'
'Mon Oct 3 12:12:01 2022 +0000	origin/launch/v1'
'Wed Apr 27 06:26:22 2022 +0000	origin/launch/v2'
'Tue Feb 15 14:46:55 2022 +0000	origin/launch/v3'
'Mon Might 24 16:05:45 2021 +0300	origin/release-v1'
'Tue Oct 6 14:43:56 2020 +0300	origin/release-v1.0.0'

The aim is to get all traces with branches which might be older than 2022, so 2021, 2020, 2019, and so forth. (12 months = key phrase, if that helps to achieve the primary aim) and show these traces within the command line/terminal.

Perhaps somebody may recommend tips on how to attain that? :slight_smile:

Br,


I’ve discovered a solution to remedy this situation utilizing ‘nested for loop’.

To start with I’ve transformed ‘out’ variable to string, then separated it into fields:

str1 := string(out)

s := strings.Fields(str1)

Second step was to create a string array with dates I’m concerned with:

	var strarray [6]string
	strarray[0] = "2016"
	strarray[1] = "2017"
	strarray[2] = "2018"
	strarray[3] = "2019"
	strarray[4] = "2020"
	strarray[5] = "2021"

and at last, nested for loop:

for _, v := vary s {
		for _, phrase := vary strarray {
			if phrase == v {
				fmt.Println("Discovered some outdated branches: ", v)
			}
		}
	}

It really works, however the OUTPUT is just not the one I anticipated:

go run important.go
CHECK ALL BRANCHES:
Discovered some outdated branches:  2021
Discovered some outdated branches:  2020

I questioning is it doable to output the entire line with the “key phrase” discovered within the ‘git department’ output, like:

go run important.go
CHECK ALL BRANCHES:
Discovered some outdated branches: 
'Mon Might 24 16:05:45 2021 +0300	origin/release-v1'
'Tue Oct 6 14:43:56 2020 +0300	origin/release-v1.0.0'

And if nothing was discovered:

go run important.go
CHECK ALL BRANCHES: OK!

Any recommendations?

Thanks upfront!

Cut up out into traces as a substitute. Put your goal years in a map[string]bool. Use an everyday expression to extract the 12 months from every line regexp bundle – regexp – Go Packages. Test whether or not the map incorporates the extracted 12 months as a key, and in that case, print a message about discovering the out of date 12 months and set a boolean variable to true. If the variable remains to be false after the loop, print your success message.



1 Like

Hello @mje,

Thanks to your reply! I’ve tried to use your suggestions and will efficiently get outdated branches:

    str1 := string(out)

	temp := strings.Cut up(str1, `n`)

	// Create a map with years
	var mapper = map[string]bool{
		"2016": true,
		"2017": true,
		"2018": true,
		"2019": true,
		"2020": true,
		"2021": true,
	}

	// Test whether or not the map incorporates the extracted 12 months as a key
	for _, v := vary temp {

		var notvalidID = regexp.MustCompile(`202([0-1])`)
		var notvalidID2 = regexp.MustCompile(`201([0-9])`)

		tsts := notvalidID2.FindAllString(v, -1)
		tst := notvalidID.FindAllString(v, -1)

		for _, tz := vary tst {
			if _, exists := mapper[tz]; exists {

				fmt.Printf("Discovered outdated department, 12 months: %s n", tz)
			} else {
				fmt.Printf("Handed")
			}
		}

		for _, tz := vary tsts {
			if _, exists := mapper[tz]; exists {

				fmt.Printf("Discovered outdated department, 12 months: %s n", tz)
			} else {
				fmt.Printf("Handed")
			}
		}
	}

Output:

CHECK ALL BRANCHES:
Discovered outdated department, 12 months: 2019
Discovered outdated department, 12 months: 2018
Discovered outdated department, 12 months: 2018

As an alternative of the output above is it doable to print the precise line the place the important thing(12 months) was discovered? Like:

go run important.go
CHECK ALL BRANCHES:
Discovered some outdated branches: 
'Mon Might 24 16:05:45 2021 +0300	origin/release-v1'
'Tue Oct 6 14:43:56 2020 +0300	origin/release-v1.0.0'

The second factor I’m frightened about is that it doesn’t output the duty of the “else” assertion. Thus, in case nothing was discovered, “Handed” needs to be displayed, however it’s not:

go run important.go
CHECK ALL BRANCHES:  //"Handed" needs to be right here if nothing was discovered

Thanks upfront!

Ought to be

                allGood := true
		for _, tz := vary tst {
			if _, exists := mapper[tz]; exists {

				fmt.Printf("Discovered outdated department, 12 months: %s n", tz)
                                allGood = false
			} 
		}
                if allGood {

				fmt.Printf("Handed")

		}



1 Like

Hello @mje,

Thanks to your reply with examples! That helped to unravel a problem with if/else assertion.

Do you could have any concepts tips on how to get the anticipated “branches output”?

go run important.go
CHECK ALL BRANCHES:
Discovered some outdated branches:
‘Mon Might 24 16:05:45 2021 +0300 origin/release-v1’
‘Tue Oct 6 14:43:56 2020 +0300 origin/release-v1.0.0’

Is that not what v incorporates?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments