As the saying goes, there are many ways leading to Rome;
Kai Burghardt has shown one.
Your capitalization request, as based on your example (i.e.: capitalize all words as a "preformat" action), can be done with GNU sed extensions; see
5.6 regular expression extensions - \b and
5.9.2 Upper/Lower case conversion:
Rich (BB code):
[1-0] % gsed -E 's/\b(.)/\U\1/g' a
London
New York
Rome
sed(1) does not support either
\b (word designation) or
\U (case converion command). If at all possible with
sed(1), this would require extensive construction of sed loops and of the
y command. Using
awk(1) for this task for example is much more straighforward:
Rich (BB code):
[2-0] % awk '{ for ( i=1; i <= NF; i++) $i = toupper(substr($i,1,1)) substr($i,2); print }' a
London
New York
Rome
For your main OP question:
| Insert stdin at a specific line |
sed 'LINE_NUMBER r /dev/stdin' target.txt <<EOF ... EOF |
but ths means nothing to me. Can anyone explain this instruction?
Taken literally (ad verbatim) this does not work because in this use of a heredoc the termination string must be the only contents of its last line; see
2.7.4 Here-Document. The correct way:
Rich (BB code):
$ sed '1 r /dev/stdin' b <<EOF
> London
> New York
> Rome
> EOF
Amsterdam
London
New York
Rome
Zurich
Basically what you are asking in your OP problem was the merging of
two inputs, thereby avoiding any temporary files—not something that is used often. As shown in the last example the
sdtin input stream and the file
b are used as inputs, both specified on the command line. Your example problem can therefore be solved without temporary files and only
sed(1) and
awk(1):
Rich (BB code):
[3-0] % awk '{ for ( i=1; i <= NF; i++) $i = toupper(substr($i,1,1)) substr($i,2); print }' a | sed '1 r /dev/stdin' b
Amsterdam
London
New York
Rome
Zurich
However, as mentioned in the beginning, using a base-install, the use of an
awk(1) script is far more appropriate for a capitalization. Furthermore, when the line number at which the "preformat[ted]" input stream is to be inserted at a specific location becomes more complex, it would also be more practical to not use sed for that, but use another solution, for example use
awk(1) there too.