Ubuntu – Using sed to add text to a file before and after word

bashcommand linesed

I have a file with words, one word per line, which I now want to change so that is becomes a .sh-file with an egrep-search string for each word. The search string I want in the end looks like this:

`egrep -wi '\|WORD\.\.nn' stats_all.txt > WORD_frekvens.txt`

My word file looks like this:

$ more -10 word_file.txt
anakonda
ord
tröja
bord
glas
pension
larm
risk
försening
rapport

I have tried to do this with the following string:

sed -e 's/\(.*\)/egrep -wi '\''\\|\1\\.\\.nn'\'' stats_all.txt > \1_frekvens.txt/' word_file.txt | more

But it gives the output:

_frekvens.txt_all.txt > WORD

Why doesn't it work to use the \1 here? It seems to be this part that's the problem, as it works if I exchange it for a word.

Anything I write after \1 ends up writing over the content of \1, i.e. "WORD". If I write something only before \1 it works fine.

Very grateful for any help.

Best Answer

You can also process your list of words with the following perl command:

$ perl -ne 's/\s*$//; print "egrep -wi \047\|${_}\.\.nn\047 stats_all.txt > ${_}_frekvens.txt\n"' word_file.txt

Note that I'm using \047 to avoid escaping ' (single quote).

With a dummy word_file.txt:

foo
bar

The output is:

egrep -wi '\|foo\.\.nn' stats_all.txt > foo_frekvens.txt
egrep -wi '\|bar\.\.nn' stats_all.txt > bar_frekvens.txt
Related Question