In your linked question there is already good awk
answer, just modify it a little bit by using printf
instead of print
to insert the content without newline:
awk '/First/ { printf $0; getline < "File1.txt" }1' infile.txt
Result:
Some Text here
FirstThis is text to be inserted into the File.
Second
Some Text here
You may want to add space or other delimeter after "First" with printf $0 " "; ...
If inserted file has many lines then:
awk '/First/{printf $0; while(getline line<"File1.txt"){print line};next}1' infile.txt
Result:
Some Text here
First[
{
"foo": "bar",
"baz": "biff",
"data": [
{
"a": 1945619,
"b": [
{
"c": 512665,
"d": "futz"
}
]
}
]
}
]
Second
Some Text here
Here's one that works in the simple case of only one level of commands within an \edit{...}
, at maximum:
perl -00 -lpe 's,\\edit\{( (?: [^}\\]* | \\[a-z]+\{[^}]*\} )+ )\},$1,xg'
The middle part (?: [^}\\]* | \\[a-z]+\{[^}]*\} )+
has to alternatives:
[^}\\]*
matches any string with no closing brace or backslash (regular text); and \\[a-z]+\{[^}]*\}
matches anything with backslash, lowercase letters, and then a matched pair of braces (like \url{whatever...}
). The grouping (?:...)+
repeats those alternatives, and the outer parenthesis capture, so we can replace the match with just the part inside \edit{...}
.
-00
tells Perl to handle the input one paragraph at time, with empty lines separating paragraphs. If you need to handle tags that span paragraphs, change that to -0
to handle the whole input in one go.
For your example, this seems to work, giving:
We Introduce a model for analyzing \emph{data} from various
experimental designs, such as paired or \url{http://www/}
longitudinal; as was done 1984 by NN \cite{mycitation} and by NNN
\cite{mycitation2}.
However, it (predictably) fails for an input with two levels of commands inside the \edit{...}
:
Some \edit{\somecmd{\emph{nested} commands} here}.
Turns to:
Some \somecmd{\emph{nested} commands here}.
(the wrong closing brace is removed)
Actually handling balanced parenthesis is somewhat more tricky, it's discussed e.g. in this question on SO:
Perl regular expression: match nested brackets.
Best Answer
1 at the end means
{print}
.If you want to extend it to more numbers,
awk
makes that easy:With different numbers, we'll just say sed isn't the right tool for the job. It's possible, but not pretty.
As an academic curiosity, here it is in sed: