Operating System - HP-UX
1830898 Members
3069 Online
110017 Solutions
New Discussion

Re: parsing shell variable to vi

 
SOLVED
Go to solution
mahesh_24
Occasional Advisor

parsing shell variable to vi

is there any way to parse a variable defined in shell and use in vi editor or reference it usng $user .
8 REPLIES 8
Umapathy S
Honored Contributor

Re: parsing shell variable to vi

Mahesh,
I am not able to understand your question correctly. Explain the question a little bit.

Why do you want to use a variable in a vi editor? How?

-Umapathy
Arise Awake and Stop NOT till the goal is Reached!
mahesh_24
Occasional Advisor

Re: parsing shell variable to vi

i am supplying one variable in the shell script as input , now i want to use the same variable in vi.search the pattern using that variable value and replace that with the new variable.
Mark Grant
Honored Contributor

Re: parsing shell variable to vi

mahesh,

I think you need to look at "sed". I have to admit, the man page for sed puts people off for a year or two before they start using it but a simple use might help you here.

sed "s/pattern/$variable/g" will look for all occurances of the pattern and replace it with the value of the variable. sed can be given a file name to look through for this pattern or it can take its standard input.
Never preceed any demonstration with anything more predictive than "watch this"
mahesh_24
Occasional Advisor

Re: parsing shell variable to vi

Hi Mark,
i have used sed to perform some of my task.

the requirement is to disable a user from the passwd file and the fields to be changed are

passwd filed to *AP*
username to Xusername
GECOS field to "disabled"
shell to /bin/false

now i have written some code using sed , i am pasting below but changes only the user field

sed -n '/^'$user1':/ {
s/'$user1'/X'$user1'/
p
}' passwd.


any help will be highly apprecialted.
Mark Grant
Honored Contributor

Re: parsing shell variable to vi

Sometimes it's just better to go with perl

Here is a easy to read perl script.

It takes an argument of the user name you want to change and writes it to the standard output.

#!/usr/bin/perl

open INPUTFILE,"
while($line=){
chomp($line);
($login,$passwd,$uid,$gid,$gecos,$homedir,$sh)=split ":",$line;
if($login eq $ARGV[0]){
$login="X$login";
$passwd="*AP*";
$gecos="disabled";
$sh="/bin/false";
}
print "$login:$passwd:$uid:$gid:$gecos:$homedir:$sh\n";
}
Never preceed any demonstration with anything more predictive than "watch this"
Patrick Van Humbeeck
Valued Contributor

Re: parsing shell variable to vi

or maybe with awk ?

here's a skeletton script :)

awk -F":" '$1 ~ /USERNAME/ { print "X" $1 ":*AP*:" $3 } $1 !~ /USERNAME/ { print }' /etc/passwd

my example will only change the first 3 fields, but you get the idea - if first field matches you do all changes you want in there with print $field and if it does not match then you just print the line untouched. this produces an updated passwd file.
mahesh_24
Occasional Advisor

Re: parsing shell variable to vi

Thanks Partrik But here there something needs to be done as eg the user name here is 'oracle" so by using the above commands it will match users like "oracledb" oracledba etc .
Patrick Van Humbeeck
Valued Contributor
Solution

Re: parsing shell variable to vi

mahesh,

you can solve this problem easily by matching $1 ~ /^USERNAME$/ instead of /USERNAME/.

Rgds,

Patrick