我想为给定文档的每一行打印人员的名字和年龄(这是文档的条目)。我会使用什么linux命令?希望基于使用正则表达式模式的 grep。
输入文档示例:
Linda:F:Las Vegas, USA:18:telnet
Steve:M:Leipzig, Germany:33:ftp
Henry:M:Milan, Italy:19:sftp
Alice:F:Izmir, Turkey:32:pop3s
Robert:M:Verona, Italy:21:scp
Olga:F:Gorontalo, Indonesia:19:pop3s
Kimberly:F:Lille, France:25:ping
Henry:M:Oslo, Norway:26:ftp
Carol:F:New Delhi, India:23:pop3s
Thomas:M:New Delhi, India:32:http
答案1
您可以使用cut
:
cut -d: -f1,4 file
Linda:18
Steve:33
Henry:19
Alice:32
Robert:21
Olga:19
Kimberly:25
Henry:26
Carol:23
Thomas:32
答案2
您可以使用awk
:
$ awk -F':' '{print $1,$4}' file
Linda 18
Steve 33
Henry 19
Alice 32
Robert 21
Olga 19
Kimberly 25
Henry 26
Carol 23
Thomas 32
答案3
sed 's/\([^:]*:\)[^:].*:\([^:]*\):.*/\1\2/' < file
Linda:18
Steve:33
Henry:19
Alice:32
Robert:21
Olga:19
Kimberly:25
Henry:26
Carol:23
Thomas:32