DevanDevak
DevanDevak

Reputation: 41

Iterate over files and update rows in directory, preferably using Bash script

I have 300 text files in a directory in following format

 regional_vol_WM1.txt

    651328 651328
    553949 553949
    307287 307287
    2558 2558       



regional_vol_WM2.txt

    651328 651328
    553949 553949
    307287 307287
    2138 2138

I would like to iterate over all these txt files and perform calculation on 4th row both columns using the formula

(Value of fourth row /0.824198)*0.8490061

and a new file with a new name regional_vol_WM2_prop.txt should be created,with first three rows having same value and fourth row with updated value with newly calculated output

Any suggestions appreciated.

Upvotes: 0

Views: 95

Answers (1)

Danny Daglas
Danny Daglas

Reputation: 1491

#!/bin/bash

awk '
FNR == 1 {
  newfilename = FILENAME ; sub(".txt", "_prop.txt", newfilename)
  printf "" > newfilename
}
FNR == 4 {
  $1=($1/0.824198)*0.8490061
  $2=($2/0.824198)*0.8490061
}
{
  print >> newfilename
}
' regional_vol_WM*[0-9].txt

Upvotes: 2

Related Questions