1838240 Members
3456 Online
110125 Solutions
New Discussion

please help

 
Ken Lee_1
Occasional Contributor

please help

data_1:1a:2b:3c:4d:5e:6f:7g:8h:9i:10j:

data_1_th:11A:12B:13C:14D:15E:16F:17G:18H:19I:20J:

assuming "data_1" and "data_1_th" is group name and 1a,2b....11A,11B...are the member of "data_1" and "data_1_th" respectively.

What I need now is to grep the group name and once the group name was grep I need to go to the last entry of the of the particular group and place a new entry there and ends with ":". All I need now is to grep the correct group name and remember that the group is a continous data and it is not fixed! Please advise. Thanks in advance.
2 REPLIES 2
Robin Wakefield
Honored Contributor

Re: please help

Hi Ken,

You could use sed in a script:

#!/bin/sh
sed "s/\(^$1:.*\)/\1$2:/" yourfile > yourfile.new
mv yourfile.new yourfile

run the script using:

yourscript.sh group new_entry

Or are you saying that data_1 and data_1_th are related and can only have 10 entries per line?

Rgds, Robin.
Tim D Fulford
Honored Contributor

Re: please help

1 - to grep data_1 & not get data_1_th use

grep ^data_1:

2 - to write you a script I would use awk

#!/usr/bin/ksh
file=group-file
echo "Input user to be added ==>\c"
read user
echo "Input group for user to be added to ==>\c"
read group

awk -F":" '$1==GROUP { for (i=1; i<=NF; i=i+1) printf "%s:", $i ; printf "%s:\n", USER } $1!=GROUP { for (i=1; i<=NF-1; i=i+1)
printf "%s:", $i ; printf "%s:\n", $NF } ' USER=$user GROUP=$group $file > $file.tmp
mv $file.tmp $file

** PLEASE NOTE ** The group file you have listed above will NOT be /etc/group, as this has a totally different format. If you do mean this file then the script needs to be modified!

Tim
-