英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论