Issue
I'm trying to run a sed script in which the following line isn't working.
from sedfile.sed file:
s|^NEWLINE|XYZ|g
I'm unable to figure it out but why?
I am on windows and I am running it using a batch file.
Command is being issued as: preprocess_answers.bat temp.text
contents of preprocess_answers.bat
:preserving newline characters
fart -C %1 \r\n NEWLINE
sed -i -E -f preprocess_answers.sed %1
contents of preprocess_answers.sed
#usage: sed -i -E -f preprocess_answers.sed filename.tex
s|(NEWLINE *[0-9]+\. *)|\n\1|g
s|Hint|\tHint|g
#s|^NEWLINE([^\r\n\t]+)|\\begin{finalanswer} \1 \\end{finalanswer}|g
s|^NEWLINE|XYZ|g
s|\t(Hint[^\r\n]+)|\t\\begin{hint} \1 \\end{hint}|g
contents of temp.text
1. (b)
2. (c)
3. (c)
4. (a)
5. (b)
1. asdf Hint: asdfasdf
1. asdf Hint: asdfasdf
1. asdf Hint: asdfasdf
1. asdf Hint: asdfasdf
1. asdf Hint: asdfasdf
after runing the command, the text file looks like this:
1. (b)
NEWLINE2. (c)
NEWLINE3. (c)
NEWLINE4. (a)
NEWLINE5. (b)
NEWLINE1. asdf \begin{hint} Hint: asdfasdf \end{hint}
NEWLINE1. asdf \begin{hint} Hint: asdfasdf \end{hint}
NEWLINE1. asdf \begin{hint} Hint: asdfasdf \end{hint}
NEWLINE1. asdf \begin{hint} Hint: asdfasdf \end{hint}
NEWLINE1. asdf \begin{hint} Hint: asdfasdf \end{hint}
EDIT:
I am trying to get the commented out line: s|^NEWLINE([^\r\n\t]+)|\\begin{finalanswer} \1 \\end{finalanswer}|g
to work, but since it was failing, in the process of debugging, I found that ^
is the culprit.
Solution
sed is a stream editor, and it loads each line of the stream into the pattern space, then executes all the commands in your .sed
file, then loads the next line, etc.
Except you have removed all linebreaks and replaced them with 'NEWLINE'. So you are loading everything into the pattern space and processing everything at once. (You can do with with sed -z
, or with $!N;${...your commands}
, or with branching.)
In any event, ^
matches the beginning of the pattern space, not the beginning of every line. I'd recommend using \b
to mean 'word boundary' because it's zero-space. ie: \bNEWLINE
You also ought to see whether dos2unix
and unix2dos
exist on your system to deal with the whole DOS linebreak thing.
Answered By - stevesliva