In all the many years using this site I’ve never had to ask a question because there has ALWAYS been an answer (usually numerous). I’m pretty sure this one Does too but for the life of me I cannot find it.
I have directory with a bunch of files which have numerous lines of random length.
a.txt
b.txt
c.txt
d.txt
Then I have single fileeg.txt
with a set list of strings
opq 111
rst 222
uvw 333
xyz 444
Each of the txt files has a single string I’d like to replace
a.txt has a#P#b
b.txt has c#P#d
c.txt has e#P#f
d.txt has g#P#h
I want to replace #P#
with the second ‘column’ from my file of strings. The #P#
occurs only one time per file (because I’ve put it there).
The result would be
a.txt has a111b
b.txt has c222d
c.txt has e333f
d.txt has g444h
The ‘constant‘ assumption is that there are as many lines ineg.txt
as there are.txt
files in my directory and they are in Alphabetical order. The lines ineg.txt
are sorted alphabetically as per ‘column’ 1
I’ve been trying to do it using awk
and sed
(well actually sd
) within a for loop but I’m failing to get it to read both ‘source’ and ‘target’ line by line.
I’m not fussy as to how I achieve the result. Currently I’m not working with many lines or files (15 lines and 15 files right now) but there will be times where there will be quite a lot more. I am using zsh as my shell on both an Arch & Debian based linux distro (WSL 2 at times)
Apologies if this has an answer. I’ve really tried to find it over the last two days while working on this project and my brain is now spent.
EDIT: Updated to clarify that the files in the directory have numerous lines of various length and that my given string #P#
occurs only once per file