0

I try to save changes in files in awk I have files like

Eq6_1.ndx
eq6_2.ndx
...
...
eq6_10.ndx

This is working ok

for index in {1..10}
do
    awk 'f;/hbonds_Other-SOL/{f=1}' eq6_$index.ndx | tee eq7_$index.ndx 
done

But I want to save changes in the same file, so I try this

for index in {1..10}
do
    awk 'f;/hbonds_Other-SOL/{f=1}'  "eq6_$index.ndx" > "$tmp" && mv "$tmp" "eq6_$index.ndx"  
done

But I have information that there is no such file In the end I want to try this

#!/bin/bash
name="eq6"
for index in {1..10}
do
    awk 'f;/hbonds_Other-SOL/{f=1}'  "${name}_$index.ndx" > "$tmp" && mv "$tmp" "{$name}_$index.ndx" 
done

but I have information that there is no such file? What I do wrong I try this solution and it's not working how to write finding output to same file using awk command

Jakub
  • 679
  • 5
  • 16
  • As I've mentioned in comments to your previous questions (e.g. https://stackoverflow.com/questions/65970828/instead-of-null-print-zero-in-awk#comment116642611_65970828), you don't need to call awk in a loop. If you're interested in learning how to do what you're doing orders of magnitude more efficiently then ask a new question about that. Otherwise I'll assume you're not interested and try to remember not to keep suggesting it if you post other questions. – Ed Morton Jan 31 '21 at 14:33
  • https://www.shellcheck.net/ would have told you about your current problem by the way - always run `shellcheck` on your shell scripts before posting questions. – Ed Morton Jan 31 '21 at 14:39
  • @EdMorton I create a new question about that https://stackoverflow.com/questions/65980443/instead-for-loop-in-bash-through-all-files-do-this-in-awk I want to learn new things so why not? – Jakub Jan 31 '21 at 14:46
  • 1
    I see you deleted it again. If you do decide to undelete it make sure to include your awk version in it (try `awk --version`) and if it's not gawk then tell us if you can install gawk. – Ed Morton Jan 31 '21 at 15:30

1 Answers1

2

tmp is a variable, you have to actually set it to the name of a file before trying to access that file with $tmp:

tmp=$(mktemp) || exit 1
name='eq6'
for index in {1..10}
do
    awk 'f;/hbonds_Other-SOL/{f=1}'  "${name}_$index.ndx" > "$tmp" && mv "$tmp" "${name}_$index.ndx" 
done

You also had {$name} instead of ${name} but I assume that was a typo.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185