add text before the last line in a text file - windows

I need to add text before the last line in a text file in windows using command. Can any one please suggest a method?
Thanks in advance.

You can do it in 5 simple steps, and you can use any language of your choice or according to your other requirements ( like c, c++, java etc.)
Read Complete File
Extract Last line by looking for last newline
Store Last Line and erase it from file
Append New Text
Append last line which you deleted previously.
Don't forget to close your file.

Related

Read the final line in a text file

I am currently learning Go, and I need to read the final line in a text file.
I have searched everywhere and there does not seem to be a definitive explanation on how one would do this.
How would I do this?
Starting the search from the very beginning of the file can be an expensive option esp. if your file(s) are large.
A better option may be to - Use os.Open to open the file and stat method (https://golang.org/pkg/os/#File.Stat) to get the size of the file. Start reading from end of the file using ReadAt (https://golang.org/pkg/os/#File.ReadAt - read the last byte first, second last byte next..), all the way reverse till you find the second newline character. That's the beginning of the last line. Hope this helps.

How to compare a lines of a text file with a line of different text file and append the first one if they are not same

I want to compare particular line of two text file and update one of the file if they are not same.
Updating a line in a text file is technically not possible (unless the replacing line is of exactly the same length). You have to create a new file, which you can, in the end, move to the old one.
From your tags, I assume that you are looking for a shell solution, which is maybe not a good idea. It's probably more convenient to do it in, for instance, Perl or Ruby or Python.
One possibility is to use the commands head and tail, which allow you to dissect a file into parts. You can split your file into three parts: The part before the line in question, the line itself, and the lines which come after.
Another possibility is to use a loop and the read command of the shell to process a file line by line, like this:
while read line
do
... # Decide here, whether to write $line or the replacement line
done <your_file

Read file starting at second line LabView

Using the Read from Text File Function I am able to easily read the first line of my file. However I now want it to read the second line. It would be great to just a for loop or something if I could specify the line number somewhere. Is there a way to do so? Thanks!
First, you can read the entire file as lines by right-clicking on the Read From Text File node and selecting "Read Lines". One read will return an array containing one element for each line and you can work with the lines with regular array handling methods. If you want to read each line individually, you can by wiring a 1 into the Count input and looping. Each iteration will return an array with one element (the current line read). You can get/set the offset (in bytes) to specify where in the file you want to read, but that's not necessary if I read your question correctly.

Is there a way to delete all comments in a file using Notepad++?

Notepad++ obviously recognizes all comments as such. Is there a way to simply delete all?
Edit: Stat-R's bookmark method has helped greatly, not only for removing comments but for conditionally removing lines in general.
For a general file, first of all you need to know the comment operator of the language you are writing the file in. For example, in java script the comment operator is //.
For the following code...
In NP++, you need to
Mark the lines that contains '//'. Make sure the bookmark option is enabled.
Then, choose from NP++ menu Search>Bookmark>Remove Bookmarked lines
EDIT:
Another solution after #Chris Mirno 's suggestion is as follows:
Use regular expression. See the image below. It is self explanatory
To understand it better, refer to these
In the Find & Replace Dialog, put the following regex and adjust the search options as depicted.
/\*.*?\*/
Replace with: (empty)
Select Mode: Regular Expression AND .(dot) matches newline
This should remove all your C style comments spanned across lines.
Star-R and Chris Mirno Answer are also Correct and Good.
But For Line Comment:
//.*?(?=\r?$)
Explanation:
// will be the Starting Position
.*? Will be any character
(?=\r?$) will search to the end of the line (as it is required in line comment)
Note:
But Still check each of the line because for example if your code contains soap format like
//www.w3.org/2001/XMLSchema-instance\x2......");
it will capture this line because the starting is // and it goes to end of the line so watch out for this :)
Warning to all using Stat-R's solution:
This method will remove lines of code if formatted like this:
echo "hello"; //This comment will be detected
Following his method, the entire line will be removed.
Therefore make sure to go through and make these comments, their own line before doing this method.
I have had some luck running a macro for the above. Basically:
search for // (F3)
select to end of line (shift+end)
delete (delete)
Put // into the search dialog by just searching for it once. Then record the three steps in a macro, then play it back until EOF.
The first time I did it I had a problem, but then it worked, not sure what I did differently.
Anton Largiader's answer was the most reliable one, including complex inline comments.
However, it will leave many empty lines, including ones with empty characters (space, tabs...) so I would just add another step to make it almost perfect:
After running the macro, just do:
Edit > Line Operations > Remove Empty Lines
OR
Edit > Line Operations > Remove Empty Lines (Containing Blank Characters)
1st option is good if you wish to remove only really empty lines
2nd options will remove every empty line even containing space etc. so there will be no more actual spacing left between code blocks. 1st option might be the safest with some manual cleanup afterwards.
As someone suggested in another post, the simplest and most reliable is maybe to export the all text in .RTF format using Menu Plugin-->NppExport-->Export to RTF and then:
-Open the newly created file in Word
-Select any part of any comment
-On the top-right side of Word clic Select--> Select all texts with similar formatting
-Remove the selected comments all at once (del or cut if doesn't work)
To remove Powershell comments if someone find it handy:
Removing Comment in a Powershell using Notepad ++
To find just lines beginning with # (and not with # elsewhere in the line).
Notepad++ SEARCH Menu > Find
‘Mark‘ Tab – fill in as below.
Select ‘Mark All’ (clear all marks if used previously).
Regex ^[#}
enter image description here
SEARCH Menu > bookmark > Remove (or do anything on the list with
them)
Clear all marks to reset
You can select no comments just code by doing the following:
Regex ^[^#}
enter image description here
Enter ctrl+shift+K to remove comment

Reading previous line of file with Ruby

Using Ruby, I am reading a file line by line, using IO.gets to incrementally read the next line of the file. Under certain circumstances I want to do the opposite (look at the previous line by decrementing). The way I tried to accomplish this was...
IO.lineno = int
IO.gets
It seems that no matter what I set "lineno" to equal it still just reads the next line when I follow up by calling "gets". How should I go about reading previous lines in the file?
You need to use
IO.readlines("myfile")
This returns the file as an array of strings and then iterate over it with indizies. With a stream there is no way to go back one line.

Resources