Thursday, March 23, 2023
HomeGolangFilter a textual content by the key phrase - Getting Assist

Filter a textual content by the key phrase – Getting Assist


Hello neighborhood,

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

bundle principal

import (
	"fmt"
	"os/exec"
)

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

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

	str1 := string(out)

	fmt.Println(str1)
}

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

And getting:

go run principal.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 objective is to get all traces with branches which might be older than 2022, so 2021, 2020, 2019, and so on. (12 months = key phrase, if that helps to achieve the primary objective) and show these traces within the command line/terminal.

Possibly somebody might recommend the right way to attain that? :slight_smile:

Br,


I’ve discovered a solution to resolve this challenge 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 thinking about:

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

and eventually, 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 just isn’t the one I anticipated:

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

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

go run principal.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 principal.go
CHECK ALL BRANCHES: OK!

Any ideas?

Thanks prematurely!

Break up out into traces as a substitute. Put your goal years in a map[string]bool. Use a daily expression to extract the 12 months from every line regexp bundle – regexp – Go Packages. Examine whether or not the map comprises the extracted 12 months as a key, and if that’s the 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 on your reply! I’ve tried to use your suggestions and will efficiently get outdated branches:

    str1 := string(out)

	temp := strings.Break 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,
	}

	// Examine whether or not the map comprises 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 a substitute of the output above is it potential to print the precise line the place the important thing(12 months) was discovered? Like:

go run principal.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 anxious 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 principal.go
CHECK ALL BRANCHES:  //"Handed" needs to be right here if nothing was discovered

Thanks prematurely!

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 on your reply with examples! That helped to resolve a difficulty with if/else assertion.

Do you may have any concepts the right way to get the anticipated “branches output”?

go run principal.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 comprises?

Hello @HowardParks,

Sure, that’s proper. But when I specify v right here fmt.Printf("Discovered outdated department, 12 months: %s n", v) it’ll present an output:

3. CHECK ALL BRANCHES:
Discovered outdated department: '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.4'
'Fri Sep 30 12:00:51 2022 +0000	origin/growth'
'Wed Apr 27 06:26:22 2022 +0000	origin/launch/v1.3'
'Tue Feb 15 14:46:55 2022 +0000	origin/launch/v1.2'
'Mon Might 24 16:05:45 2021 +0300	origin/release-v1.1'
'Tue Oct 6 14:43:56 2020 +0300	origin/release-v1.0.0'

Discovered outdated department: '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.4'
'Fri Sep 30 12:00:51 2022 +0000	origin/growth'
'Wed Apr 27 06:26:22 2022 +0000	origin/launch/v1.3'
'Tue Feb 15 14:46:55 2022 +0000	origin/launch/v1.2'
'Mon Might 24 16:05:45 2021 +0300	origin/release-v1.1'
'Tue Oct 6 14:43:56 2020 +0300	origin/release-v1.0.0'

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments