Jakub
Jakub

Reputation: 699

Problem with the save changes in the same file with awk

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

Upvotes: 0

Views: 234

Answers (1)

Ed Morton
Ed Morton

Reputation: 203577

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.

Upvotes: 2

Related Questions