Best way to edit a file

looking for a script or command to push out from a centralized machine to multiple machines. I have the software in place that will do this already, but I need to tell it what command to run on each machine with this file.

what I need is a script or command to edit a file in a specific manner. Say I have a file such as the one below. What I need to do is search the file for a specific line, in this case the line "users_here[tab space]u1=usera,userb,userc", and delete the text "usera,". I don't want it to remove ALL instances of "usera" throughout the file, and I don't want to output this to a new file. I need to search the file for the line, remove 'usera' if it's in THAT specific line, and leave the file intact after it edits/removes "usera" from that line. Any help with this?

#
user_goHere
user2_goHere
users_here[tab space]u1=usera,userb,userc
users_there[tab space]u2=user1,user2,user3

here is more text in the file that also contains usera, but i don't want it delete this one. here's another usera, but i don't want it to remove this either.
and here is some more text in the file.
#

sed -i 's/users_here[tab space]u1=usera,userb,userc/users_here[tab space]u1=userb,userc/' inputFile

If -i option doesn't work then

sed 's/users_here[tab space]u1=usera,userb,userc/users_here[tab space]u1=userb,userc/' inputFile > temp
mv temp inputFile

ok, I should have been a bit more clear. Some of versions of this file can be varying on some machines. The one identical factor is that the line on every machine will start with 'users_here' (for example, the string after 'u=' might be varying on some machines). So I need a script/command that will search the file for the line starting with 'users_here', and if there happens to be a 'usera' in that line, I want it removed.

I would like to just edit this file and not have to add another step of moving a new file into it's place, if possible.

:confused::confused::confused:

1st sed command with -i switch should work for you.
If not,

 
sed -i 's/users_here\(.*\)usera,\(.*\)/users_here\1\2/' inputFile

If usera also could be the last word in the line (without a trailing comma):

awk '!f && /users_here/{sub("usera[,]*","");f=1}1' file

running both "sed -i" commands results in:
sed: illegal option -- i

Then you need to use a temp file.

 
sed 's/users_here\(.*\)usera,\(.*\)/users_here\1\2/' inputFile > temp
mv temp inputFile

running this I get:

awk: syntax error near line 1
awk: bailing out near line 1

awk is not working for you. Use nawk instead of awk

 
nawk '!f && /users_here/{sub("usera[,]*","");f=1}1' file

neither seem to work, get errors with both.

Helpful!

There's no reason why nawk shouldn't work.

Try /usr/xpg4/bin/awk.

Otherwise it would be helpful to know

  • What you mean by "error" (as there shouldn't be any)
  • What the error is

Oh and

  • What OS you are using (I assumed it was Solaris)

using nawk (yes, with Solaris), here's the error I get:

nawk: syntax error at source line 1
context is
!f && >>> /users_here/{sub(usera,* <<< ,),f=1}1
nawk: illegal statement at source line 1

Looks like the command you ran is not correct. Please verify that again with above suggestions and
if doesn't work, post the command which is giving above error.

The output from the error doesn't seem to reflect the code that Franklin posted. Did you type it in, or copy and paste it?

A slight change, although functionally equivalent is:

awk '!f && /users_here/{sub(/usera,*/,"");f=1}f' file

Did you also try /usr/xpg4/bin/awk?