3

I did a lot of research about this question but none of the solutions proposed has beed useful. I have two files, alpha (which is without extension, it's a openfoam dictionary) and beta.txt. I would replace a block of strings in alpha with the content of beta.txt. The content of alpha is this:

Some text which must not be changed
Some text which must not be changed
Some text which must not be changed
Some text which must not be changed
Amin     0.3;
Bmin     0.1;
Cmin     0.4;
vertices
Some text which must not be changed
Some text which must not be changed
Some text which must not be changed
Some text which must not be changed

The content of beta is:

Amin     0.7;
Bmin     0.4;
Cmin     0.1;
vertices

I would edit permanently the file alpha (not only in the terminal) by substituting the block of beta in place of Amin ... vertices, Both numerical values of alpha and beta could change but the names "Amin" "Bmin" "Cmin" and "vertices" are constant. I have tried perl as:

perl -i -p0e 's/Amin.*?vertices\n/`cat beta.txt`/se' alpha.txt

but it doesn't work. Note that there are newlines, tabulations and semicolons. Maybe they are not included. Thank you in advance!

6
  • What do you mean by "it doesn't work" ? What errors or unexpected results do you get ? – Soren A Sep 24 '20 at 9:27
  • It launches the command but nothing happens. I know that .* means all the characters but how can I include semicolons and tabulations that are all along the block? – furystep Sep 24 '20 at 9:32
  • Does alpha.txt have DOS-style (CRLF) line endings by any chance? that would cause your regex not to match – steeldriver Sep 24 '20 at 9:43
  • Actually, alpha is not a txt file but a dictionary of openfoam (blockMeshDict which has not extension) and I can't pass it as blockMeshDict.txt – furystep Sep 24 '20 at 10:42
  • 1
    Please edit your question to include the output of file alpha.txt (or file blockMeshDict as appropriate) – steeldriver Sep 24 '20 at 11:13
2

Your command does work for me. Try running with export LC_ALL=C to remove any language issues. And add -w to your perl command to turn on warnings. Check that there is no whitespace after vertices in alpha.txt. You may want to hexdump your input: xxd alpha.txt to check for invisible bytes that may be preventing your regex from matching, such as the carriage-returns that @steeldriver suggested.

Your test will also be easier if you don't run with -i (inplace replacement) but instead send your output to stdout, which you can redirect to a different file if you'd like. That way you can more easily repeat your test if something goes wrong. Once your command is working, you can add -i back.

Here's what I ran:

$ cat tt
cat alpha.txt
echo
export LC_ALL=C
perl -wp0e 's/Amin.*?vertices\n/`cat beta.txt`/se' alpha.txt

Here's my output

$ tt
Some text which must not be changed
Some text which must not be changed
Some text which must not be changed
Some text which must not be changed
Amin     0.3;
Bmin     0.1;
Cmin     0.4;
vertices
Some text which must not be changed
Some text which must not be changed
Some text which must not be changed
Some text which must not be changed

Some text which must not be changed
Some text which must not be changed
Some text which must not be changed
Some text which must not be changed
Amin     0.7;
Bmin     0.4;
Cmin     0.1;
vertices
Some text which must not be changed
Some text which must not be changed
Some text which must not be changed
Some text which must not be changed

By the way, you must have done a lot of research to come up with your command. That command is a lot more advanced than I would expect from a first time questioner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.