开发者

Regex, how can I grab just the first line of output?

开发者 https://www.devze.com 2023-01-23 14:04 出处:网络
My output has more than 1 line, so I am trying to match the linebreak at the end of the line and then 开发者_开发技巧have only 1 max occurance. I was trying something like this: ^$output*\\n$\\{,1\\}

My output has more than 1 line, so I am trying to match the linebreak at the end of the line and then 开发者_开发技巧have only 1 max occurance. I was trying something like this: ^$output*\n$\{,1\} but that didn't really work out right.


You can grab the first line of output using the Unix utility head

head -1


What language? In sed: sed -e 's/:/!/g' -e '1q' /etc/passwd The second command means "quit on line 1".

In perl you can use the m flag to treat one long, multi-line string as multiple lines and then $ will match the first newline.

In awk you could either make the line number part of the condition: awk '/.../ && NR == 1 { print }' or you could quit after the first line: awk '/.../ { print } { exit }'

With grep you could limit the file with head: head -1 file | grep pattern or limit to the first matching line with grep -c 1 pattern file...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号