I am working on a task to edit a Linux text file. Everything is working as expected but I would like to sort the lines of the file alphabetically before saving it. Is it possible to manipulate the __appendfile variable in place or will this process require the creation of an additional temporary file? I have searched the forum but have not found an answer.
The requirements for the sorting process are:
Exclude the first, second, and last lines from the sorting process based on line number, not content
Sort the lines based on the text before the “=” sign. (All sorted lines will contain the “=” sign)
If possible, the sorting should be done AFTER the __appendfile variable has been fully populated
I would prefer to use native BES code only
Sample contents:
# First Line
# Second Line -----------------
Sort_line_B = 0
Sort_line_K = Not set
Sort_line_A = 5
Sort_line_Z = 1
Sort_line_M = Description
# Last line --------------------
I have no clue how to do this so any sample code or reference articles would be much appreciated.
I’m not sure I get the requirement if I’m honest but __appendfile is essentially the creation of a file on the endpoint so it will create the file in the way that it’s written in the fixlet. If you reorder it in the fixlet then when it gets to the endpoint it will be the same.
As John has said, the creation of the file is done by your code, so why not write the lines in the order you want them?
It is certainly possible to do, but if you only want to use actionscript it would be part of creating the file - i.e. it would be written in the correct order
Agree with everyone here it’s a strange use-case.
You can’t sort an existing file in-place but you can move it to a temp file and generate a new one. I’m away from a computer but I think this should get you close, using the side-effect that ‘unique values of’ sorts the results.
Delete __tmpfile
Move __appendfile __tmpfile
Appendfile {line 0 of file "__tmpfile"}
Appendfile {concatenation "%0d%0a" of unique values of (it as string) of lines (integers in (1, number of lines of it - 1 ) of it ) of files "__tmpfile"}
Appendfile {lines (number of lines of it) of files "__tmpfile"}
Allow me to elaborate.
Here is the action Script code I am currently using:
appendfile { parameter "File_Description" }
appendfile { parameter "Separator_Line" }
appendfile { concatenation ( parameter "LF_CR" ) of lines whose ( line number of it >= 1 and ( ( it contains ( parameter "Find_SubString__PROPERTY_EQUALS" ) ) and ( it != ( parameter "Find_SubString__PROPERTY_EQUALS" ) ) and ( it does not contain ( parameter "Find_SubString__PROPERTY_NAME" ) ) and ( it does not contain ( parameter "Find_Separator_Subtring" ) ) and ( it != ( parameter "EOF_Delimeter" ) ) ) ) of file ( parameter "File_Name" ) }
appendfile { parameter "Find_SubString__PROPERTY_NAME" & parameter "SubString_PROPERTY_VALUE" }
appendfile { parameter "EOF_Delimeter" }
In an effort to eliminate errors or “mis-formatting” as a result of misguided user intervention directly on the file, I have chosen to rewrite the file every time it is eddied.
Referring to the code above, I do the following:
Explicitly create the first 2 lines of the file
Grab the contents of the existing file and keep only the lines that contain " = " and are NOT EQUAL to " = " (SPACE + = + SPACE).
Explicitly create the line I want to add or edit
Explicitly create the last line of the file
As part of this process, how can I sort the content I grab + the PROPERTY line I add or edit, before storing it in the __appendfile variable? i. e. Lines 3 and 4 of the code above
A code sample would be most useful as I am still in the early stages of the learning curve.
if {exists file "c:\trn\sortedstuff.txt"}
parameter "stuff"="{concatenation "%0d%0a" of unique values of lines whose (it does not start with "#" and it does not start with "Sort_line_X" and it as trimmed string != "=") of file "c:\trn\sortedstuff.txt"}"
else
parameter "stuff"="{}"
endif
delete __appendfile
appendfile # First Line
appendfile # Second Line -----------------
appendfile {parameter "stuff"}
appendfile Sort_line_X = {now}
appendfile # Last line --------------------
delete "c:\trn\sortedstuff.txt"
copy __appendfile "c:\trn\sortedstuff.txt"
delete __appendfile