Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
601 views
in Technique[技术] by (71.8m points)

split - Go lang differentiate " " and line break

I am trying read certain string output generated by linux command by the following code:

out, err := exec.Command("sh", "-c", cmd).Output()

The above out is of []byte type, how can I differentiate the " " character contained in line content with the real line break? I tried

strings.Split(output, "
")

and

bufio.NewScanner(strings.NewReader(output))

but they both split the whole string buffer whenever seeing a " " character.

OK, to clarify, an "unreal" break is a " " character contained in a string as follows, Print first result: "123; 234; " Print second result: "456; "

The whole output is one big multi-line string, it may also contain some other quoted strings, and I am processing the whole string output in my go program, but I can't control the command output and add a back slash before the " " character.

Further clarify: I meant to process byte sequence which contains string of strings, and want to preserve the " " contained in the inner string and use the the outer layer " " to break lines. So for the following byte sequence:

First line: "test1"
Second line: "123;
234;
345;"
Third line: "456;
567;"
Fourth line: "test4"

I want to get 3 lines when processing the whole sequence, instead of getting 7 total lines. It's a old project, but I remember I can use Python to directly get 3 lines using syntax like "for line in f", and print the content of second inner string instead of rendering it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...