File append if exist or not exist

Hi Team,

I want to append/add one line entry in the exist file on windows & unix environment.

File name :: ClientAgent.cfg

Entry to be added ::SITE_CONTROLLER_ADDRESS=192.x.x.x

Here I want to check one condition like whether we have any line entry present SITE_CONTROLLER_ADDRESS=
( without any IP address assigned ) and add my new IP address .

kindly help me in this request .

As example:

rest.protocol=https
rest.port=8082
SITE_CONTROLLER_ADDRESS=192.x.x.x

@goperiya1

Try this:
https://bigfix.me/fixlet/details/26946

on edit: I forgot to mention that I only wrote it for Windows, but it shouldn’t be too hard to extrapolate to Linux/Unix. Let me know if you need help

It’s crude, but it works. You can take it and make it pretty by changing the hard path filename with a parameter or anything else you like.

It’s only relevant on computers where:

  1. The file “c:\temp\ClientAgent.cfg” exists
  2. The file “c:\temp\ClientAgent.cfg” has a line containing “SITE_CONTROLLER_ADDRESS” and its value is empty

What it does:

  1. When you take the action it asks you for the IP Address you want to assign to that field

  2. dumps the content of the “ClientAgent.cfg” file to a __createfile (without the line containing “SITE_CONTROLLER_ADDRESS” and appends a new “SITE_CONTROLLER_ADDRESS” field along with the value of the IP Address you specified

  3. Deletes the old “ClientAgent.cfg” file

  4. Moves the __createfile to be the new “ClientAgent.cfg” file

In case you have trouble seeing the item in bigfix.me:

Relevance:

clause 1: exists file “C:\temp\ClientAgent.cfg”
clause 2: following text of first “=” of (lines of file “c:\temp\ClientAgent.cfg” as string) whose (it contains “SITE_CONTROLLER_ADDRESS”) = “”


Action Script:


action parameter query “ipaddress” with description “Enter the value to add to the ‘SITE_CONTROLLER_ADDRESS’ field:”

delete __createfile

createfile until EOF
{concatenation “%0D%0A” of ((it) as string) whose (it does not contain “SITE_CONTROLLER_ADDRESS=”) of (lines of file “c:\temp\ClientAgent.cfg”)}
SITE_CONTROLLER_ADDRESS={parameter “ipaddress”}
EOF

dos del /F “c:\temp\ClientAgent.cfg”

move __createfile “C:\temp\ClientAgent.cfg”


I hope that helps. If you find an error and need some help just post here again.

Mike.

2 Likes

Hi Mike/Team,

Please see my below update and help me.

I will quickly describe my requirement in the better way.

I need to append IP address in the below configuration file .

sample config file :: Filename :: panacesagent.cfg

rest.protocol=https
rest.port=8082
SITE_CONTROLLER_ADDRESS=

Before that, I need to check one condition .

Condition :

panacesagent.cfg contains any line entry “SITE_CONTROLLER_ADDRESS=” (whether it’s already exists or not)

If file have exist entry of SITE_CONTROLLER_ADDRESS= then, add one IP address in the end of = sign ( SITE_CONTROLLER_ADDRESS= x.x.x.x)

else

If doesn’t have line entry ( “SITE_CONTROLLER_ADDRESS=” )

add one new line entry
append SITE_CONTROLLER_ADDRESS=x.x.x.x

We need to use parameter option for IP inputs .

We need to take a action on all AIX, Linux, Solaris & windows environment. ( common action required… Based on OS type, action has to trigger ).

Kindly help me .

I haven’t really tested this, but I think the following should hopefully get you started for the actionscript portion:

action parameter query "sitecontrolleraddress" with description "Please enter Site Controller Address IP value:"

delete __appendfile
appendfile {concatenation "%0d%0a" of ((lines whose (it as lowercase does not start with "site_controller_address=") of it;"SITE_CONTROLLER_ADDRESS=" & parameter "sitecontrolleraddress" of action) of file "R:\panacesagent.cfg")}

This will query the operator for the desired IP address value, then prepare an appendfile (which will later have to be moved to replace the .cfg file) that contains all the lines of the cfg file except any that contain “site_controller_address”, and append the additional line as specified.

The script is not working as per my expectation.

action parameter query “sitecontrolleraddress” with description “Please enter Site Controller Address IP value:”

delete __appendfile
appendfile {concatenation “%0d%0a” of ((lines whose (it as lowercase does not start with “PANACES_SITE_CONTROLLER_ADDRESS=”) of it;“PANACES_SITE_CONTROLLER_ADDRESS=” & parameter “sitecontrolleraddress” of action) of file “/tmp/scripttest/PanacesAgentGeneric.cfg”)}
copy “/tmp/scripttest/PanacesAgentGeneric.cfg” “/tmp/scripttest/PanacesAgentGeneric.cfg.backup”

move __appendfile “/tmp/scripttest/PanacesAgentGeneric.cfg”

Its adding one more line entry in the file below.

cat PanacesAgentGeneric.cfg

PANACES_MASTER_SERVER_ADDRESS=10.x.x.x
PANACES_SLAVE_SERVER_ADDRESS=10.x.x.x
PANACES_AGENT_NODE_ADDRESS=10.x.x.x
PANACES_AGENT_NODE_BIND_ADDRESS=
PANACES_SITE_CONTROLLER_ADDRESS= ( this suppose to add IP here )
IS_SERIALCALL_ENABLED=false
PANACES_SITE_CONTROLLER_ADDRESS=10.x.x.x ( instead , its added one more entry in the last line )

My requirement is

if file has already line entry has ( PANACES_SITE_CONTROLLER_ADDRESS= ), use the same input value ( PANACES_SITE_CONTROLLER_ADDRESS= ) & use parameter to append the new IP

If file doesn’t have ( PANACES_SITE_CONTROLLER_ADDRESS= ) entry, add new line with new IP .

I need output in the below format ::

cat PanacesAgentGeneric.cfg

PANACES_MASTER_SERVER_ADDRESS=10.x.x.x
PANACES_SLAVE_SERVER_ADDRESS=10.x.x.x
PANACES_AGENT_NODE_ADDRESS=10.x.x.x
PANACES_AGENT_NODE_BIND_ADDRESS=
PANACES_SITE_CONTROLLER_ADDRESS=10.x.x.x
IS_SERIALCALL_ENABLED=false

%0d%0a is the hex-encoded ASCII for Carriage Return/Line Free (end-of-line symbols on Windows). Add these before the new line in the relevance above.

Hi Jason,

Sorry I am not getting you. Please help me to fix this problem . Thanks.

Do you mean, remove this "%0d%0a" from action ?

appendfile {concatenation "%0d%0a" of ((lines whose (it as lowercase does not start with “site_controller_address=”) of it;“SITE_CONTROLLER_ADDRESS=” & parameter “sitecontrolleraddress” of action) of file “R:\panacesagent.cfg”)}

No, that %0d%0a is what puts the newlines between the lines…you need another one

appendfile {concatenation "%0d%0a" of ((lines whose (it as lowercase does not start with "site_controller_address=") of it;"%0d%0aSITE_CONTROLLER_ADDRESS=" & parameter "sitecontrolleraddress" of action) of file "R:\panacesagent.cfg")}

On Linux/UNIX/Mac you’ll probably need to use %0a instead of %0a, as those operating systems use a different end-of-line character for text files. So you’ll need separate blocks in your action based on

if {Windows of operating system}
    // Use the action above
Else
    // Use the action above with %0a instead of %0d%0a and use a different file path
Endif

Thanks Jason. Will test and share you the result.

It’s not working. Same result.

#cat PanacesAgentGeneric.cfg
PANACES_MASTER_SERVER_ADDRESS=10.x.x.x
PANACES_SLAVE_SERVER_ADDRESS=10.x.x.x
PANACES_AGENT_NODE_ADDRESS=10.x.x.x
PANACES_SITE_CONTROLLER_ADDRESS= ( either this entry has to be removed atleast )
PANACES_AGENT_NODE_BIND_ADDRESS=
PANACES_SITE_CONTROLLER_ADDRESS=10.x.x.x
IS_SERIALCALL_ENABLED=false

That is because your ‘requirements’ changed halfway through this thread.
Just change the bits that refer to ‘site_controller_address’ to you new value

Yes. was not expected this situation. Now I have to either remove those line .

Is that possible to change the code and fix this issue? I need support.

Everything you need is in the previous post. Change the strings you were asking us to change before, to match the strings you’re asking us to change now.

I have tried many ways to resolve the current my issue. all ideas failing. Badly I need support

Hint: You are casting the line to lowercase, but then comparing to an uppercase string…

1 Like