Append text to the end of a line - text

I'm working with AutoIt, and I was wondering in there is a method I can use to append a string of text to the end of a line inside a text file. I've been browsing all over autoit forums and there are lots of answers that are really close, but I have not found a solution that has actually worked for me.
The function:
FileWriteLine($LOG, "FText")
just adds a whole new line at the bottom, while the function:
_FileWriteToLine($LOG, 1, "FText", 0)
adds the letters "FText" to the beginning of the first line in the log file.
Is there any way I can add this text to the end of the first line, instead of the beginning?

I have never come up with this problem but just thinking of it, how about reading the whole line, storing it in a variable, add the extra test you want in the end of the line and then write the new line as it is modified replacing the old line???

You can use the FileWrite function: FileWrite documentation
You can create your own file writing function to include the opening, writing, and closing of the file like this:
Func WriteToLog($FileName, $Value)
$FileHandle = FileOpen($FileName, 1) ; 1 = append mode
If $FileHandle <> -1 Then
FileWrite($FileHandle, $Value)
EndIf
FileClose($FileHandle)
EndFunc
Then using your example and assuming $LOG is the file name for your log file, you can simply call your function whenever you need to log something:
WriteToLog($LOG, "FText")

Related

How to add comments to multiple lines in VIM?

I know how to comment out multiple lines in VIM, but what if I wanted to add comments in the end of each line?
For example:
function dir.ls(path)
local i,files = 0,{}
local pfile = io.popen('ls "'..path..'"')
for fname in pfile:lines() do
i = i + 1
fpath = path..fname
files[i] = fpath
end
pfile:close()
return files
end
Now with added comments:
function dir.ls(path)
local i,files = 0,{}
local pfile = io.popen('ls "'..path..'"')
for fname in pfile:lines() do
i = i + 1
fpath = path..fname -- your comment goes here
files[i] = fpath -- your comment goes here
end
pfile:close() -- your comment goes here
return files
end
Append your comment to the first line:
A -- your comment goes here<Esc>
Move the cursor to the next line you want to add a comment to.
Repeat the last edit:
.
And so on…
In your example:
A -- your comment goes here<Esc>
j.
jj.
Another method, but in a single step:
:,+3v/end/norm A -- your comment goes here<CR>
That command is easier to understand if it is explained from right to left:
The :normal command allows you to execute a sequence of normal mode commands from command-line mode. Here, we use it to append the comment to the given line, just like in the first step of the multi-step method.
v/pattern/command is a companion to the :global command. It means "run the given command on every line in the given range that doesn't match pattern". Here, we run our :normal command on every line in the given range that doesn't contain end.
,+3 is the range of lines on which we want to run the :v command. It is a shortened version of .,+3 which means "the current line and the next three lines".

Linux application in livecode

Spell Check is a default application in Linux. With the help of that application, can we check the spelling of a text field while users enter data?
Some (or many?) Linux distributions contain a command line utility that is called spell. If you run this with words as parameters, you need to press return a second time, but if you use a file as a paramater, you don't need to press return again. This means that a solution could be:
write the text of a field to a file
run the command line utility from LiveCode's shell function with the file as parameter
parse the result returned by the shell function
Before you try this, open your terminal on Linux and type spell. Press enter to see if the command is recognised. If yes, then the script below should work.
This script writes the text of a field to a file, does a spell check on the file and returns the incorrect words to LiveCode. I haven't tested the script and you may have to tweak it a little.
function spellCheck theText
// works on Linux only
if the platform is "Linux" then
// remove everything that isn't a word
put replaceText(theText,"[^\w]","") into myWords
// write clean data to a temporary file
put the tempName into myTempFile
put myWords into url ("file:" & myTempFile)
// call spell with shell
put "spell" && myTempFile into myShell
// only return the incorrect words
put line 2 to -1 of shell(myShell) into myCorrections
// return the incorrect words to calling handler
return myCorrections
else
// this isn't Linux
return "error"
end if
end spellCheck
//theField is the short name of a field
on checkField theField
// call above function
put spellCheck(the text of fld theField) into myWords
// myWords should now contain the incorrect words
if myWords is not "error" then
lock screen
// parse incorrect words and mark them in the field
repeat with x = 1 to number of words of field theField
if myWord is among the lines of myWords then
// an incorrect word has been found and is marked red
set the textColor of word x of fld theField to red
end if
end repeat
unlock screen
end if
end checkField
Usage: checkField shortNameOfTheField

Adding a newline character within a cell (CSV)

I would like to import product descriptions that need to be logically broken according by things like description, dimensions, finishes etc. How can I insert a line break so that when I import the file they will show up?
This question was answered well at Can you encode CR/LF in into CSV files?.
Consider also reverse engineering multiple lines in Excel. To embed a newline in an Excel cell, press Alt+Enter. Then save the file as a .csv. You'll see that the double-quotes start on one line and each new line in the file is considered an embedded newline in the cell.
I struggled with this as well but heres the solution. If you add " before and at the end of the csv string you are trying to display, it will consolidate them into 1 cell while honoring new line.
csvString += "\""+"Date Generated: \n" ;
csvString += "Doctor: " + "\n"+"\"" + "\n";
I have the same issue, when I try to export the content of email to csv and still keep it break line when importing to excel.
I export the conent as this: ="Line 1"&CHAR(10)&"Line 2"
When I import it to excel(google), excel understand it as string. It still not break new line.
We need to trigger excel to treat it as formula by:
Format -> Number | Scientific.
This is not the good way but it resolve my issue.
supposing you have a text variable containing:
const text = 'wonderful text with \n newline'
the newline in the csv file is correctly interpreted having enclosed the string with double quotes and spaces
'" ' + text + ' "'
On Excel for Mac 2011, the newline had to be a \r instead of an \n
So
"\"first line\rsecond line\""
would show up as a cell with 2 lines
I was concatenating the variable and adding multiple items in same row. so below code work for me. "\n" new line code is mandatory to add first and last of each line if you will add it on last only it will append last 1-2 character to new lines.
$itemCode = '';
foreach($returnData['repairdetail'] as $checkkey=>$repairDetailData){
if($checkkey >0){
$itemCode .= "\n".trim(#$repairDetailData['ItemMaster']->Item_Code)."\n";
}else{
$itemCode .= "\n".trim(#$repairDetailData['ItemMaster']->Item_Code)."\n";
}
$repairDetaile[]= array(
$itemCode,
)
}
// pass all array to here
foreach ($repairDetaile as $csvData) {
fputcsv($csv_file,$csvData,',','"');
}
fclose($csv_file);
I converted a pandas DataFrame to a csv string using DataFrame.to_csv() and then I looked at the results. It included \r\n as the end of line character(s). I suggest inserting these into your csv string as your row separation.
Depending on the tools used to generate the csv string you may need escape the \ character (\r\n).

Remove newline while writing to text

This simple macro will write the text "ABC" to the file temp.txt under E: drive. However if we open the text file, notice that there is a new line char after C. How can we get rid of this, so that it will end after C
Code:
Sub ExamplePrint
i = FreeFile()
Open "e:\Temp.txt" For Output As i
Print #i, "ABC"
Close #i
end Sub
In vba if we give like this, the newline doesnt come. however in openoffice the whole text document comes as empty
Code:
Print #i, "ABC";
Try the FileSystemObject:
http://msdn.microsoft.com/en-us/library/6ee7s9w2%28v=vs.85%29.aspx

vim how to create a debug message fast

I am using ruby on rails but that does not matter much for this question. Let's say that i have a statement like this
error = 'this is an error message'
I have noticed that I end up doing this a lot
error = 'this is an error message'
puts "error = #{error.inspect}"
I am sure a macro can be written which would take the work on the left hand side of left most = and then create another line along with template shown above.
I am using mvim on mac. Any pointer in terms of where I should start to look for developing what I want.
Try snipmate:
http://www.vim.org/scripts/script.php?script_id=2540
I recorded a simple macro that does your sample. To record a macro type q followed by what register you want the macro to be put in (convention calls for qq). To play the macro type # then the macro register. You can view this at :help recording
To write the macro, use the following commands (and here is how is should look in the register)
^yEoputs "error = #{^Op.inspect}"^[
^ moves to the first non whitespace character of the line
yE yanks to the end of the space separated word.
o Puts you in insert mode on the next line
puts "error = #{ is the text that you type out
^O is ctrl+O (capital letter o) - this allows the next, and only the next command to be run in command mode, which is...
p Puts the yanked word, after this command is run you're still in insert mode
.inspect}" is the text that you type and finally...
^[ is Esc
I would go for:
nnoremap µ :s/^\s*\(\k\+\)\s*=.*/&\rputs "\1 = #{\1.inspect}"/<cr>
:s presents the advantage of doing the job plus matching the assigned variable if any. Doing the same thing with classical commands like yw, p, etc would be more cumbersome.
If the template become more complex, we can rely on template-file expanders as long as they easily permit to call viml function like matchstr(). Of course, in that case I would use mu-template with the following template-file:
VimL:" $Id: {rtp}/template/ruby/inspect.template
VimL: let s:value_start = '¡'
VimL: let s:value_end = '¡'
VimL: let s:reindent = 1
VimL: let s:marker_open = '<+'
VimL: let s:marker_close = '+>'
VimL: let s:varname = matchstr(getline(line('.')-1), '^\s*\zs\k\+\ze\s*=')
VimL: if empty(s:varname) |throw "the previous line don't assign any variable" |endif
puts "¡s:varname¡ = #{¡s:varname¡.inspect}"<++>
VimL:"vim: encoding=utf-8
If you're doing these on the fly, a snipmate snippet could look like this:
${1:error} = '${2:error message here}'
puts "error = #{$1.inspect}"
If, on the other hand you're just wanting to output pre-existing variables for debugging purposes. Nick-Canzoneri's macro may be more useful.

Resources