Mike
Mike

Reputation: 1

How do I change a group of group IDs in Linux

I have a large /etc/group. All GIDs between 100 - 999 I want to change them by adding a 9 to the beginning of it and all other group IDs I want to leave alone. Ex.

group1:x:12:
group2:x:123:
group3:x:234:
group4:x:678:
group5:x:1234:

Should become.

group1:x:12:
group2:x:9123:
group3:x:9234:
group4:x:9678:
group5:x:1234:

I've tried to do this with awk and sed but some things are not clear to me how to do. Please help. Thank you.

Upvotes: 0

Views: 215

Answers (2)

Cyrus
Cyrus

Reputation: 88949

With GNU sed:

sed -r 's/:([0-9]{3}:[^:]*$)/:9\1/' file

Output:

group1:x:12:
group2:x:9123:
group3:x:9234:
group4:x:9678:
group5:x:1234:

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 204558

$ awk 'BEGIN{FS=OFS=":"} $3>=100 && $3<=999 {$3="9"$3} 1' file
group1:x:12:
group2:x:9123:
group3:x:9234:
group4:x:9678:
group5:x:1234:

Upvotes: 1

Related Questions