0

First, I'm in Korea. So my English will very bad.

I want to edit third line with out touching first line and the second line.

Before:

echo sample
echo sample
echo sample
echo sample

After:

echo sample
echo sample
echo hello
echo sample

Just like this. without using this command:

echo echo sample> test.txt
echo echo sample> test.txt
echo echo hello> test.txt
echo echo sample> test.txt

So, I mean is not write 1,2,4's 'echo' and 'sample' things. Only the third line's 'echo'. What can I do?

Paul R
  • 208,748
  • 37
  • 389
  • 560
Korean
  • 1
  • 1
  • I suggest you [install cygwin](http://cygwin.com) and then you can use standard *nix tools such as sed and awk, which makes tasks like this very simple. – Paul R Mar 29 '17 at 07:43
  • 1
    it's not possible to "edit" a file in `cmd`. You have to read and rewrite the whole file line by line (with a [for](https://ss64.com/nt/for_f.html)). – Stephan Mar 29 '17 at 13:16

2 Answers2

0

You can find the answer to your question here:

bash: replace an entire line in a text file

In your case, the command to use is:

sed -i '3s/.*/echo hello/' test.txt
Community
  • 1
  • 1
  • sorry, but I'm talking about the windows command prompt. It's not working. can you know about the windows command prompt command? – Korean Mar 29 '17 at 10:20
0

This will edit the 2nd line in C:\test.txt, just change the 3rd line down to the line number you want to edit.

@echo off
setlocal enabledelayedexpansion
set i=1
for /f %%a in (C:\test.txt) do (
if !i!==2 (
echo your new line >>C:\test2.txt
) else (
echo %%a >>C:\test2.txt
)
set /a i+=1
)
del C:\test.txt
ren C:\test2.txt test.txt
Bali C
  • 30,582
  • 35
  • 123
  • 152