从多行输出中提取日期(Go语言)

huangapple go评论94阅读模式
英文:

Extract date out of multiline output in Go

问题

请查看下面的net use命令的输出。现在我想从这段文本中提取出到期日期。不幸的是,net use命令无法输出为json、xml或其他可解析的格式。因此,我只能使用这段文本 :(. 我只对获取10-6-2017 6:57:20并将其转换为Golang日期格式感兴趣。

问题是:我不知道该如何开始?首先找到包含"Password expires"的行?然后呢?

User name                    jdoe
Full Name                    John Doe
Comment
User's comment
Country code                 (null)
Account active               Yes
Account expires              Never

Password last set            1-5-2017 6:57:20
Password expires             10-6-2017 6:57:20
Password changeable          1-5-2017 6:57:20
Password required            Yes
User may change password     Yes
英文:

Please see the output of the net use command below. Now I want to extract the expiration date out of this piece of text. Unfortunately the net use command is unable to output as json, xml or whatever parsble format. Therefore I'm stuck to this text :(. I'm only interested in getting 10-6-2017 6:57:20 and convert it into Golang date format.

The problem: I don't know how to start? First find the row which contains "Password expires"? And then what?

User name                    jdoe
Full Name                    John Doe
Comment
User's comment
Country code                 (null)
Account active               Yes
Account expires              Never

Password last set            1-5-2017 6:57:20
Password expires             10-6-2017 6:57:20
Password changeable          1-5-2017 6:57:20
Password required            Yes
User may change password     Yes

答案1

得分: 1

请稍等,我会为您翻译代码部分。

import (
	"fmt"
	"strings"
)

func main() {
	str := `
User name                    jdoe
Full Name                    John Doe
Comment
User's comment
Country code                 (null)
Account active               Yes
Account expires              Never

Password last set            1-5-2017 6:57:20
Password expires             10-6-2017 6:57:20
Password changeable          1-5-2017 6:57:20
Password required            Yes
User may change password     Yes`

	lines := strings.Split(str, "\n")

	for _, line := range lines {
		if strings.HasPrefix(line, "Password expires") {
			elems := strings.Split(line, " ")
			date := elems[len(elems)-2]
			time := elems[len(elems)-1]
			fmt.Println(date, time)
		}
	}
}

Alternatively you could use `regex`.

这是您要翻译的代码。

英文:

Here you go:

import (
	"fmt"
	"strings"
)

func main() {
	str := `
User name                    jdoe
Full Name                    John Doe
Comment
User's comment
Country code                 (null)
Account active               Yes
Account expires              Never

Password last set            1-5-2017 6:57:20
Password expires             10-6-2017 6:57:20
Password changeable          1-5-2017 6:57:20
Password required            Yes
User may change password     Yes`

	lines := strings.Split(str, "\n")

	for _, line := range lines {
		if strings.HasPrefix(line, "Password expires") {
			elems := strings.Split(line, " ")
			date := elems[len(elems)-2]
			time := elems[len(elems)-1]
			fmt.Println(date, time)
		}
	}
}

Alternatively you could use regex.

答案2

得分: 0

例如,假设日期格式为dd-mm-yyyy,时间格式为24小时制,本地时间为阿姆斯特丹:

package main

import (
	"bufio"
	"fmt"
	"strings"
	"time"
)

func passwordExpires(netuser string) time.Time {
	const title = "Password expires"
	scanner := bufio.NewScanner(strings.NewReader(netuser))
	for scanner.Scan() {
		line := scanner.Text()
		if !strings.HasPrefix(line, title) {
			continue
		}
		value := strings.TrimSpace(line[len(title):])
		format := "2-1-2006 15:04:05"
		loc := time.Now().Location()
		expires, err := time.ParseInLocation(format, value, loc)
		if err == nil {
			return expires
		}
	}
	return time.Time{}
}

// Europe/Amsterdam
var netuser = `User name                    jdoe
Full Name                    John Doe
Comment
User's comment
Country code                 (null)
Account active               Yes
Account expires              Never

Password last set            1-5-2017 6:57:20
Password expires             10-6-2017 6:57:20
Password changeable          1-5-2017 6:57:20
Password required            Yes
User may change password     Yes`

func main() {
	expires := passwordExpires(netuser)
	fmt.Println(expires)
	if expires.IsZero() {
		fmt.Println("no password expiration")
	}
}

输出:

>go run expire.go
2017-06-10 06:57:20 +0200 CEST
英文:

For example, assuming dd-mm-yyyy, 24-hour clock, and local time location (Amsterdam),

package main

import (
	"bufio"
	"fmt"
	"strings"
	"time"
)

func passwordExpires(netuser string) time.Time {
	const title = "Password expires"
	scanner := bufio.NewScanner(strings.NewReader(netuser))
	for scanner.Scan() {
		line := scanner.Text()
		if !strings.HasPrefix(line, title) {
			continue
		}
		value := strings.TrimSpace(line[len(title):])
		format := "2-1-2006 15:04:05"
		loc := time.Now().Location()
		expires, err := time.ParseInLocation(format, value, loc)
		if err == nil {
			return expires
		}
	}
	return time.Time{}
}

// Europe/Amsterdam
var netuser = `User name                    jdoe
Full Name                    John Doe
Comment
User's comment
Country code                 (null)
Account active               Yes
Account expires              Never

Password last set            1-5-2017 6:57:20
Password expires             10-6-2017 6:57:20
Password changeable          1-5-2017 6:57:20
Password required            Yes
User may change password     Yes`

func main() {
	expires := passwordExpires(netuser)
	fmt.Println(expires)
	if expires.IsZero() {
		fmt.Println("no password expiration")
	}
}

Output:

>go run expire.go
2017-06-10 06:57:20 +0200 CEST

huangapple
  • 本文由 发表于 2017年6月2日 04:03:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/44316189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定