Databases
cancel
Showing results for 
Search instead for 
Did you mean: 

Changing file contents using script

SOLVED
Go to solution
jagdeesh
Frequent Advisor

Changing file contents using script

Hi,
i have a file containing 4 feilds separated by a : . This is used by application to determine the usrer rights. File look like this

user1:lp:3:FLb//*xKns:
user3:lp:0:ClbkWTlqmYY:
user4:lp:3:OyNWlqPZxxE:
user5:lp:4:XyyNpqrzT/K:

The same file existing in almost 200 systems running HP-UX 11i. Now requirement is to change the user rights in feild 3. The number varies from 0 - 4. How can i achieve this using a script. Any help is highly appriciated.

Thanks in advance
...Jag
3 REPLIES
H.Merijn Brand (procura
Honored Contributor

Re: Changing file contents using script

in my opinion, but I'm probably a bit prejusiced, Perl is the obvious choise

# perl -pi -aF: -e'$F[2]++;$_=join":",@F' your_file

to increment all third fields (perl counts from 0)

# perl -pi -aF: -e'$F[0]=~/9/ and $F[2]=9;$_=join":",@F' your_file

to set field 3 to 9 if the first field has a 9 in it

# perl -pi -aF: -e'$F[0]=~/6$/ and $F[2]+=4;$_=join":",@F' your_file

to add 4 to field 3 if the first field ends with a 6

# perl -pi -aF: -e'BEGIN{%u=(user2=>4,user8=>3)}exists$f{$F[0]} and $F[2]=$u{$F[0]};$_=join":",@F' your_file

to set field 3 to 4 for field1 equal to user2, field 3 to 3 for field1 equal to user8, and field 3 unchanged for all other users

Due to the -pi option, all changes are in-place, meaning there are no pipes or temporary files involved

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Victor Fridyev
Honored Contributor
Solution

Re: Changing file contents using script

Hi,

You can try with awk as well:

TMP=/tmp/$$
awk -F: {$3=whatyouneed;print}' $SOURCE |sed 's/ /:/g'>$TMP
cp $TMP $SOURCE

Naturally, you will have to define the variable "whatyouneed" according to your needs.

HTH
Entities are not to be multiplied beyond necessity - RTFM
curt larson_1
Honored Contributor

Re: Changing file contents using script


userid="user1"
uperms=2

if (( $uperms > -1 && $uperms < 5 )) ;then
ex -s -c "s/\(^$userid:.*:\)[0-4]/\1$uperms/ |wq" yourfile
fi

you might want to make a back up copy of your file before making your modification just in case