0

I'm using GNU Sed 4.2.1. I'm trying to replace the second field in the following line (the password in /etc/shadow). Awk is not an option.

username:P@s$w0rDh@$H:15986:0:365::::

I've tried

sed -i 's/\(^[a-z]*\):.*?:/\1:TEST:/'

but nothing. I've tried many variations but for some reason I can't get it to only match that field. Help?

4

2 回答 2

3

用于[^:]*匹配所有内容,直到下一个:

sed -i 's/^\([^:]*\):\([^:]*\):/\1:TEST:/'
于 2013-10-11T17:04:15.610 回答
1

使用 sed:

sed -i.bak 's/^\([^:]*:\)[^:]*\(.*\)$/\1foo\2/' file

使用 awk 你可以:

awk -F: '{$2="foo"}1' OFS=: file
于 2013-10-11T17:04:44.863 回答