使用sed匹配第5个字段中的文本 [英] Using sed to match text in the 5th field

查看:39
本文介绍了使用sed匹配第5个字段中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图在/etc/passwd的第5个字段中查找某些单词.例如:

So, I am trying to look for certain words in the 5th field of /etc/passwd. For example:

jonesc:x:1053:1001:Cathy Jones:/export/home/jonesc:/bin/ksh
smiths:x:1049:1000:Sue Williams:/export/home/smiths:/bin/csh
smitha:x:1050:1001:Amy Smith:/export/home/smitha:/bin/bash

让我们说我在寻找史密斯"这个词吗?我将如何只在包含名称的第5个字段中查找它,而不是在整行中查找?

Lets say I am looking for the word 'Smith'? How would I look for it ONLY in the 5th field that contains the names, as opposed to looking through the entire line?

我可以用awk轻松地做到这一点,但是却要求我用sed来做到这一点.

I can easily do this with awk, but I am asked to do this with sed instead.

我要执行的操作是将/etc/passwd中包含第5个字段中的Smith或Jones的匹配项输出到名为smith_jones.txt的文件中.

What I'm asked to do is to output matches from /etc/passwd that contain Smith or Jones in the 5th field to a file called smith_jones.txt.

我用sed将输出写入文件没有问题,我只是停留在应该只在第5个字段中查找的方式.Awk会用$ 5,但我找不到与sed类似的东西.

I have no problem with writing output to file with sed, I am just stuck with how I am supposed to look for only in the 5th field. Awk would use $5, but I cannot find something similar with sed.

不是要寻找完整的答案,而是要朝着正确的方向前进.

Not looking for a complete answer being handed to me, but rather a push in the right direction.

推荐答案

Awk是适合该工作的工具:

Awk would be the right tool for the job:

awk '$5 ~ /smith|jones/{print}' /etc/passwd > output.txt

但是,由于您正在寻求一种sed解决方案,因此您可以使用以下方式:

But since you are asking for a sed solution then you can use something like this:

sed -n '/[^:]*:[^:]*:[^:]*:[^:]*:\(smith\|jones\)/p' /etc/passwd

:以外,每个 [^:] * 都将匹配所有零次或多次.

Where each [^:]* will match everything but : zero or more times.

您还可以使用范围元序列重复以前的模式: \ {x,y \} :

You can also repeat a previous pattern with the range meta sequence: \{x,y\}:

sed -n '/\([^:]*:\)\{4\}\(smith\|jones\)/p' /etc/passwd

如您所见,这将帮助您进一步简化正则表达式.

As you can see this will help you simplify your regex even more.

-n 默认情况下不打印,并且/pattern/p 将打印所有与 pattern

-n is for no print by default and /pattern/p will print everything matching pattern

如果要匹配用户名的中间名,则可能要在 \(smith \ | jones \)之前添加另一个 [^:] * :

You might want to add another [^:]* before \(smith\|jones\) if you want to match the middle of the user name, eg:

sed -n '/\([^:]*:\)\{4\}[^:]*\(th\|es\)/p' /etc/passwd

将匹配 Smith Jones .

正如注释中指出的那样,您还可以使用扩展正则表达式来避免所有这些反斜杠:

As pointed out in the comments you can also use Extended Regular Expressions to avoid all those backslashes:

sed -E -n '/([^:]*:){4}(smith|jones)/p' /etc/passwd

传统上,GNU sed使用 -r 来启用ERE,而BSD sed使用 -E .GNU sed即使未记录也支持 -E 标志.

Traditionally GNU sed used -r to enable ERE and BSD sed uses -E. GNU sed however support the -E flag even though it's undocumented.

这篇关于使用sed匹配第5个字段中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆