Replace the First Line in a Text File by a String

Replace only first line of text file in python

You can use the readlines and writelines to do this.
For example, I created a file called "test.txt" that contains two lines (in Out[3]). After opening the file, I can use f.readlines() to get all lines in a list of string format. Then, the only thing I need to do is to replace the first element of the string to whatever I want, and then write back.

with open("test.txt") as f:
lines = f.readlines()

lines # ['This is the first line.\n', 'This is the second line.\n']

lines[0] = "This is the line that's replaced.\n"

lines # ["This is the line that's replaced.\n", 'This is the second line.\n']

with open("test.txt", "w") as f:
f.writelines(lines)

Replace the first line in a text file by a string

sed is the right tool, try doing :

var="movie.MOV"
sed -i "1s/.*/$var/" file.txt

explanations

  • 1 mean first line
  • the rest is the substitution s/// : we substitute everything (.*) by the $var variable

Replace first line of a text file in Java

A RandomAccessFile will do the trick, unless the length of the resulting line is different from the length of the original line.

If it turns out you are forced to perform a copy (where the first line is replaced and the rest of the data shall be copied as-is), I suggest using a BufferedReader and BufferedWriter. First use BufferedReader's readLine() to read the first line. Modify it and write it to the BufferedWriter. Then use a char[] array to perform a brute-force copy of the remainder of the file. This will be more efficient than doing the copy line by line. Let me know if you need details..

Another option is to perform the reading and writing inside the same file. It'll be a bit more complex though. :) Let me know if you need details on this as well..

how to replace the first line of a file with the first line of another file

This might work for you (GNU sed):

sed -e '1R file1' -e '1d' file2

Read the first line of file2. Read the first line of file1 and insert it into the output, then delete the first line of file2. Now read and output the rest of file2.

Replace certain element in only first line of the text file

You can only read the whole file, call .replace() for the first line and write it to the new file.

with open('in.txt') as fin:
lines = fin.readlines()
lines[0] = lines[0].replace('old_value', 'new_value')

with open('out.txt', 'w') as fout:
for line in lines:
fout.write(line)

If your file isn't really big, you can use just .join():

with open('out.txt', 'w') as fout:
fout.write(''.join(lines))

And if it is really big, you would probably better read and write lines simultaneously.

How to Replace text ONCE one a specific line in text file?

a simple line counter should help:

set "search=SPL"
set "replace=TRNS"
set "textfile=Input.txt"
set "newfile=Output.txt"
set lineNr=0
(for /f "delims=" %%i in (%textfile%) do (
set /a lineNr+=1
set "line=%%i"
setlocal enabledelayedexpansion
if !lineNr!==4 set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>"%newfile%"

Sed replace a string in the first line of a paragraph

Use awk instead:

awk '/^$/{a=1} !a--{sub(/#/,"")} 1' a=1 file
  • /^$/ { a = 1 } means set a to 1 if current line is a blank one,
  • !a-- is a shorthand for a-- == 0, following action ({ sub(/#/, "") }) removes the first # from current line,
  • 1 means print all lines,
  • a=1 is required to remove # from the line after shebang (i.e 2nd line).

replace the first line of text from a file

Not a one-liner but it works (replace test1.txt and test2.txt with your paths):

.{
"<text to add>"
Get-Content test1.txt | Select-Object -Skip 1
} |
Set-Content test2.txt


Related Topics



Leave a reply



Submit