Here is my problem:
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.
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:
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?
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