Question:
Is there an easy way to put a character at the beginning and end of each line in a text file?
Daniel
2009-05-12 11:33:18 UTC
I'm trying to put a character in the beginning and end of EACH line in a text file. The file has 226 lines, and I'm just simply too lazy to the character in every single line. So does anyone know a simple way to do this?
Five answers:
HandyManOrNot
2009-05-12 11:47:33 UTC
Just open the file for modification, create a Buffer to hold the result, loop (while loop) to read each line from the file, add the special character to the buffer, add the line, add the character again, next line...



After all the lines are read in and stored in the buffer, output the Buffer back to the file, replacing the current contents.
anonymous
2009-05-12 11:49:11 UTC
In Word you can change all paragraph marks (^p) to ^p



It'll do every line but the beginning of the first line.



If not Word, you can use any text editor that can do a replace by regular expression. Just replace start of line with start of line and your character, and end of line with your character and end of line.



Or you can write a program to read the file, line by line, and write a new one with your character, the line, your character.
?
2016-10-05 11:50:52 UTC
in case you advise you desire loop over each and each letter of each and every line, some thing like on a similar time as($line = record) { @letters = cut up //, $line; foreach $l ( @letters ) { print $l; } } or some thing comparable.
femtorgon2
2009-05-12 11:54:38 UTC
You could use sed, such as:



cat myFile.txt | sed 's/.*$/X&X/w output.txt'



That would read the file myFile, add an X to the begining and end of each line, and then write it to output.txt (myFile.txt would not be changed, replace output.txt with myFile.txt to write directly back to it).
mannerssquadron
2009-05-12 11:41:21 UTC
In .net we would just do it like



MyString.Replace(Char(34), "Myextrastartcharacter" + Char(34) + "Myextraendcharacter").


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...