简体   繁体   English

使用grep和cut过滤文本文件以仅显示以'm','w'或's'开头的用户名及其主目录

[英]Use grep and cut to filter text file to only display usernames that start with ‘m’ ‘w’ or ‘s’ and their home directories

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
sys:x:3:1:sys:/dev:/usr/sbin/nologin
games:x:5:2:games:/usr/games:/usr/sbin/nologin
mail:x:8:5:mail:/var/mail:/usr/sbin/nologin
www-data:x:33:3:www-data:/var/www:/usr/sbin/nologin
backup:x:34:2:backup:/var/backups:/usr/sbin/nologin
nobody:x:65534:1337:nobody:/nonexistent:/usr/sbin/nologin
syslog:x:101:1000::/home/syslog:/bin/false
whoopsie:x:109:99::/nonexistent:/bin/false
user:x:1000:1000:edco8700,,,,:/home/user:/bin/bash
sshd:x:116:1337::/var/run/sshd:/usr/sbin/nologin
ntp:x:117:99::/home/ntp:/bin/false
mysql:x:118:999:MySQL Server,,,:/nonexistent:/bin/false
vboxadd:x:999:1::/var/run/vboxadd:/bin/false

this is an /etc/passwd file I need to do this command on. 这是一个/ etc / passwd文件,需要在其上执行此命令。 So far I have: 到目前为止,我有:

cut -d: -f1,6 testPasswd.txt | grep ???

that will display all the usernames and the folder associated, but I'm stuck on how to find only the ones that start with m,w,s and print the whole line. 它将显示所有用户名和关联的文件夹,但是我被困在如何仅查找以m,w,s开头的用户名并打印整行的问题上。

I've tried grep -o '^[mws]*' and different variations of it, but none have worked. 我已经尝试了grep -o '^[mws]*'和它的不同变体,但是没有一个起作用。

Any suggestions? 有什么建议么?

Easier to do with awk: 使用awk更容易:

awk 'BEGIN{FS=OFS=":"} $1 ~ /^[mws]/{print $1, $6}' testPasswd.txt

sys:/dev
mail:/var/mail
www-data:/var/www
syslog:/home/syslog
whoopsie:/nonexistent
sshd:/var/run/sshd
mysql:/nonexistent

Try variations of 尝试各种

cut -d: -f1,6 testPasswd.txt | grep '^m\|^w\|^s' 

Or to put it more concisely, 或者更简单地说

cut -d: -f1,6 testPasswd.txt | grep '^[mws]'

That's neater especially if you have a lot of patterns to match. 尤其是如果您要匹配的模式很多,那会更整洁。

But of course the awk solution is much better if doing it without constraints. 但是,如果没有限制,awk解决方案当然会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 grep 在 S3 存储桶上的每个 csv 文件中搜索文本 - Use grep to search text in each csv file on S3 bucket Grep仅是文本文件的一部分 - Grep only a part of a text file 我需要一个grep命令,它只从linux中的/ etc / passwd文件中提取用户名 - I need a grep command that will pull the usernames only from the /etc/passwd file in linux 使用awk或sed或cut过滤文本文件? - Filter text file using awk or sed or cut? 有没有办法使用grep(或任何其他工具)过滤文本文件,以便您可以获取包含在括号或括号中的文件的一部分? - Is there a way to filter a text file using grep (or any other tool), so that you can get a section of the file that's encased in bracers or brackets? 如何使用grep从文件中获取Skype用户名? - How to get Skype usernames from a file using grep? Grep过滤一个文本文件与另一个文本文件(非常大的文件) - Grep to filter one text file with another (Very large files) 当文本不存在于另一个文件中时,使用grep过滤以进行打印 - filter with grep to print when text is NOT present in another file 如何grep,然后从分隔列文件中剪切? - How to grep, then cut from a delimited column file? grep精确单词匹配(-w)不适用于文本文件中的文件路径 - grep exact word match (-w) does not work with file paths in a text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM