Search code examples
sedreplaceline

how to use sed to replace the Nth line with the Mth line in the same file by line numbers


Here is my problem:

  • Given a text file.
  • The data in each line isn't known.
  • Need to replace line 3 with line 5.
  • The line number to be replaced, and the line number to replace with, are given and are constant.
  • The line numbers are never the first line, or the last line.
  • The number of lines in the file doesn't change

Before running the code

A34JC
P1@%NWA]
MM1              //Line to be replaced
M@K(FD
BWKD             //Line to replace with
Z

After running the code

A34JC
P1@%NWA]
BWKD             //Data in Line 3 was replaced with the data from line 5
M@K(FD
BWKD
Z

I really don't mind using shell, sed (or awk though I am not familiar with it). From what I saw here I tried the following

sed 3s/.*/???/ file1.txt > file2.txt

I'm embarrassed to say what I have tried at the ???. I know I can manually insert a text there, but the problem is I don't know what data line 5 holds.

I also tried various replacements ways, but that was even worse.

sed 3d;5r file1.txt file1.txt > file2

in short, I am clueless.

Updates

Potong's first solution didn't work for me, but the alternative work. I now have a continuation question.

So the situation is this - I am having to repeat a very annoying process all the time. Without getting into it to much:

  • I am running Windows.
  • I have a text file containing processes Id numbers, and under each of the numbers are the settings that belong to the specific process.
  • The process ID changes every time I restart the process.
  • The process new ID automatically writes into the txt file.
  • Instead of setting the process setting from scratch, I simply copy the new random generated Process ID number, and erase the data of the settings that are just junk now.
  • There are 4 such process running at the same time. So they always add and change together.

Here is what I figured so far:

Note: I have added -i as to save the changes done of the same file

.\sed -E -i '28{:a;N;60!ba;s/[^\n]*(.*\n(.*))/\2\1/}' config.txt
.\sed -E -i '36{:a;N;64!ba;s/[^\n]*(.*\n(.*))/\2\1/}' config.txt
.\sed -E -i '44{:a;N;68!ba;s/[^\n]*(.*\n(.*))/\2\1/}' config.txt
.\sed -E -i '52{:a;N;72!ba;s/[^\n]*(.*\n(.*))/\2\1/}' config.txt

You can see 28,36,44,52 and 60,64,68,72 are series.

Here is what I need more help with:

I want to run all 4 of these commands automatically, For instance as .bat file or something similar. Can it work using .bat file?

I know cmd requires me to remove the ' from the command, and may add line instead of overwrite. It works pretty well in power shell but show do I make a .bat file to run on power shell?

I want to add a command at the end to delete everything from line 60 and onward. How do I do that?


Solution

  • This might work for you (GNU sed):

    sed -e '3{e sed -n "5p" file' -e 'd}' file
    

    Evaluates sed within sed using the e command.

    Alternative:

    sed -E '3{:a;N;5!ba;s/[^\n]*(.*\n(.*))/\2\1/}' file