1

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.

  • I think @Ron's edit has corrected the error, hasn't it? the original had un-escaped parentheses around the capture group i.e. (.*) in place of \(.*\): the former requires -r or -E mode (basic sed treats parentheses as literal). – steeldriver Jun 1 '15 at 12:10
  • Thanks. But no, I'm afraid not. I have done the search with \(.*\) all along (don't know why it ended up un-escaped in my question...). Still seems to be a problem. – Ingrid Jun 1 '15 at 14:03
  • What's your shell? – A.B. Jun 1 '15 at 15:56
  • I'm using Ubuntu via VirtualBox (if this is a shell). – Ingrid Jun 1 '15 at 15:59
  • Add the output of echo $SHELL – A.B. Jun 1 '15 at 16:06
1

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
  • You need a \ before the . : `egrep -wi '\|WORD\.\.nn' stats_all.txt > WORD_frekvens.txt` – A.B. Jun 1 '15 at 14:54
  • I have changed your example output, I hope it's ok. And a +1 =) – A.B. Jun 1 '15 at 15:23
  • Thanks. I get this output though: _frekvens.txtll.txt > WORD. Any idea what I am doing wrong? Something to do with the Ubuntu/Perl version?? I understand one can have different settings for RegExs? Maybe this has something to do with it? (As you can tell, I am not very used to working in the terminal.) – Ingrid Jun 1 '15 at 15:39
  • @Ingrid I know why you got this weird ouput. Your input file contains Windows line breaks (\r\n). Please try my updated command. – Sylvain Pineau Jun 1 '15 at 16:49
  • 1
    Oh wow, thanks! Yes, that was the answer! Had no idea the line breaks were different in Windows. But great! Thanks all you guys! All of the solutions would work great if I'd only made the word file in Linux... @A.P. – Ingrid Jun 1 '15 at 16:55
1

The following command:

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

gives the output:

`egrep -wi '\|anakonda\.\.nn' stats_all.txt > anakonda_frekvens.txt`
`egrep -wi '\|ord\.\.nn' stats_all.txt > ord_frekvens.txt`
`egrep -wi '\|tröja\.\.nn' stats_all.txt > tröja_frekvens.txt`
`egrep -wi '\|bord\.\.nn' stats_all.txt > bord_frekvens.txt`
`egrep -wi '\|glas\.\.nn' stats_all.txt > glas_frekvens.txt`
`egrep -wi '\|pension\.\.nn' stats_all.txt > pension_frekvens.txt`
`egrep -wi '\|larm\.\.nn' stats_all.txt > larm_frekvens.txt`
`egrep -wi '\|risk\.\.nn' stats_all.txt > risk_frekvens.txt`
`egrep -wi '\|försening\.\.nn' stats_all.txt > försening_frekvens.txt`
`egrep -wi '\|rapport\.\.nn' stats_all.txt > rapport_frekvens.txt`
1

As it has turned out, you use Windows line breaks (\r\n) in the file. Due to this fact, I have adjusted my commands, note the … gsub(/\r/,"") …

This is a simple task for awk:

awk 'gsub(/\r/,"") {print "`egrep -wi \047\\|" $_ "\\.\\.nn\047 stats_all.txt > "$_"_frekvens.txt`"}' <your_input_file>

to write the output in the input file:

awk -i inplace 'gsub(/\r/,"") {print "`egrep -wi \047\\|" $_ "\\.\\.nn\047 stats_all.txt > "$_"_frekvens.txt`"}' <your_input_file>

to write the output in a new file:

awk 'gsub(/\r/,"") {print "`egrep -wi \047\\|" $_ "\\.\\.nn\047 stats_all.txt > "$_"_frekvens.txt`"}' <your_input_file> > output_file

Sample with inline editing

% cat words
foo
bar

% awk -i inplace 'gsub(/\r/,"") {print "`egrep -wi \047\\|" $_ "\\.\\.nn\047 stats_all.txt > "$_"_frekvens.txt`"}' words

% cat words                                                                                               
`egrep -wi '\|foo\.\.nn' stats_all.txt > foo_frekvens.txt`
`egrep -wi '\|bar\.\.nn' stats_all.txt > bar_frekvens.txt`
  • This looks like a good solution, but for some reason this doesn't work either. I get the same kind of problem as earlier somehow. The output is: _frekvens.txt_all.txt > WORD. I must be doing something wrong, but can't figure out what it is... – Ingrid Jun 1 '15 at 15:26
  • @Ingrid Which output? =) – A.B. Jun 1 '15 at 15:27
  • Sorry, I'm not quite used to this, so I sent it off too early.. :) – Ingrid Jun 1 '15 at 15:28
  • What is the content of your input file? – A.B. Jun 1 '15 at 15:37
  • @Ingrid What is the content of your input file? – A.B. Jun 1 '15 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.