Add the characters in file

Dear friends,

in file i want to add some characters at line no 60 i.e.

the original line line is

if [ $LOGNAME != sam �a $LOGNAME != su1 �a $LOGNAME
!= su2 �a $LOGNAME != su3 ]  

hence the 60th line will be

f [ $LOGNAME != sam �a $LOGNAME != su1 �a $LOGNAME
!= su2 �a $LOGNAME != su3 �a $LOGNAME != su4 ]

Plz help me ...

thanks in advance

What is stopping you?

in sed command i am not able to add the whole line

before "]" and i actually don't know the exact command, also substitution is not exactly at the first or at the last of line, i want to add the substitution at 60th line before

plz help me out.

Like this:

cat file
if [ $LOGNAME != sam .a $LOGNAME != su1 .a $LOGNAME
!= su2 .a $LOGNAME != su3 ]
awk '{sub("su3","su3 -a $LOGNAME != su4")}1' file
if [ $LOGNAME != sam .a $LOGNAME != su1 .a $LOGNAME
!= su2 .a $LOGNAME != su3 -a $LOGNAME != su4 ]

Fixing in the file

awk '{sub("su3","su3 -a $LOGNAME != su4")}1' file > tmp ; mv tmp file
sed 's/!= su3/& �a $LOGNAME != su4 ]/' infile

Use -i for inline changes.

--ahamed

Dear ahamed and jotne,

thanks for the reply,
we have kept "su3" as reference but these are user-names they can as many as a specific user wants,

we want to add the substitution before "]" this is what i exactly want.

sed 's/\(if.*LOGNAME.*\)]/\1 �a $LOGNAME != su4 ]/'

--ahamed

1 Like
if [ $LOGNAME != sam �a $LOGNAME != su1 �a $LOGNAME
!= su2 �a $LOGNAME != su3 ]

contains a syntax error. Probably your mean:

if [ $LOGNAME != sam �a $LOGNAME != su1 �a $LOGNAME != su2 �a $LOGNAME != su3 ]

On line 60:

sed '60s/]/�a $LOGNAME != su4 ]/' file
1 Like

In case the target file is (part of) a shell script, then consider rewriting

if [ ... ]
then
  ...
fi

as

case $LOGNAME in
sam);;
su1);;
su2);;
su3);;
su4);;
*)
  ...
  ;;
esac

Further the case statement allows globbing like this

su[1-5]);;
1 Like