Update: Extending Arcege
's sed-to-sed idea, you can have a more managable layout (tabular, or ragged) for your find/repl templates, which can be inline... and there is no need for an intermediate file: (add a g
before the p'<<'EOF'
to make multiple changes per line... Note that foo
, barbar
, etc will be treated by sed
as regular expressions.
I=$'\01'; sed -nre "s/^([^ ]+) +/s$I\1$I/;s/$/$I/p"<<'EOF' |sed -f - file
foo By the middle of June,
barbar mosquitos were rampant,
foofoofoo the grass was tawny, a brown dust
barbarbarbar haze hung over the valley
EOF
(original post)
You can use sed
as an executable script, with a wrapper...
You can define your find and replace text in a seperate text file,
but for this sample it is defined in-line...
This script converts your find/repl pairs into sed expressions
and dynamically creates the executable script
I=$'\01'; set -f
printf "#!/bin/sed -f\n" > ~/bin/script.sed
while IFS= read -r line ;do
ix=$(expr index "$line" " ")
printf "s%s%s%s%s%s\n" $I ""${line:0:$((ix-1))}"" $I "${line:$ix}" $I
done <<'EOF' >> ~/bin/script.sed
$text1 By the middle of June,
$text2 mosquitos were rampant,
$text3 the grass was tawny, a brown dust
$text4 haze hung over the valley
EOF
chmod +x ~/bin/script.sed
This is the test file to be modified.
'$' is okay, as it isn't at the end of a line (for sed),
and it won't at any time be exposed to the shell
but any token which won't clash with sed
is fine. You can even use #
echo '$text1 $text2 $text3 $text4' > file
Assuming that ~/bin
is a $PATH directory, the command to run is:
script.sed file
*
as a separator in sed with double quotes. It might get expanded. I suggest|
for example. – Michał Šrajer Sep 02 '11 at 16:17