问题描述
我在写一个程序,从谁
命令提取信息和格式化输出。
正常谁
命令:
用户IP日期(IP)
用户IP日期(IP)
用户IP日期(IP)
用户IP日期(IP)
用户IP日期(IP)
用户IP日期(IP)
用户IP日期(IP)
用户IP日期(IP)
用户IP日期(IP)
用户IP日期(IP)
我的新旅程谁
命令:
用户
用户
用户
用户
用户
用户
用户
用户
用户
用户
用户
在为了做到这一点,我不得不与 GSUB
完全去除数字和一些其他的东西,整个行看起来是这样的:
`who`.gsub!(/ \\ D + /,).gsub!(/\\..../,).gsub!(/ \\(/ ,).gsub(/ \\ DEC /,).gsub(/ \\:/,).gsub(/ \\ PTS /,).gsub(/ \\ //,).gsub (/\\./,).gsub(/ \\)/,).gsub(/ \\本地主机/,).gsub(/ \\月/,).squeeze().strip
有没有更好的方式来写这个正则表达式没有这些 GSUB
的?
编辑:
更新谁
从接受的答案输出:把`who`.gsub(/ [\\ t] * /,)
输出现在看起来是这样的:
约翰
吉姆
杰克
史蒂夫
乔希
埃里克
等等..
下面三种方法。首先,请尝试用户
命令。 :)
二:
谁| SED -e'S / [\\ t] * // G'
第三:
把`who`.gsub(/ [\\ t] * /,)
这三种方法都更短,更简单。
I'm writing a program that will pull the information from a who
command and format the output.
Normal who
command:
user ip date (ip)
user ip date (ip)
user ip date (ip)
user ip date (ip)
user ip date (ip)
user ip date (ip)
user ip date (ip)
user ip date (ip)
user ip date (ip)
user ip date (ip)
My new take on who
command:
user
user
user
user
user
user
user
user
user
user
user
In order to do this, I had to completely remove the digits with gsub
and some other stuff, and the entire line looks like this:
`who`.gsub!(/\d+/, "").gsub!(/\..../, "").gsub!(/\(/, "").gsub(/\Dec/, "").gsub(/\:/, "").gsub(/\pts/, "").gsub(/\//, "").gsub(/\./, "").gsub(/\)/, "").gsub(/\localhost/, "").gsub(/\Nov/, "").squeeze(" ").strip
Is there a better way to write this regex without all these gsub
's?
EDIT:
Updated who
output from accepted answer: puts `who`.gsub(/[ \t].*/,"")
Output now looks like this:
John
Jim
Jake
Steve
Josh
Eric
etc..
Here are three approaches.. First, try the users
command. :)
Second:
who | sed -e 's/[ \t].*//g'
Third:
puts `who`.gsub(/[ \t].*/,"")
All three are much shorter and much simpler.
这篇关于.gsub(/\\..../,"")GSUB。(/ \\(/,"")GSUB(/ \\)/,"")。GSUB(/ \\本地主机/, "")< =这是极少数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!