EA00
EA00

Reputation: 633

Counter in bash script

I have a script that extracts filenames from an input file and is supposed to read each line (filename) and unzip the specified file, saving the unzipped content as individual files. However, I can't get my counter to work and just get all the unzipped files in one large file.

Input file contains a list: 
ens/484/59/traj.pdb 0.001353
ens/263/39/traj.pdb 0.004178
ens/400/35/traj.pdb 0.004191

I'm using the regex /.*?/.*?/ to extract the file that I'd like to unzip and name each output{1..40}.pdb -- instead I get one output file: output1.pdb which contains all the contents of the 40 unzipped files.

My question is: how do I correct my counter in order to achieve the desired naming scheme?

#!/bin/bash

file="/home/input.txt"
grep -Po '/.*?/.*?/' $file > filenames.txt
i=$((i+1))
structures='filenames.txt'

while IFS= read line
 do
   gunzip -c 'ens'$line'traj.pdb.gz' >> 'output'$i'.pdb'
 done <"$structures"

rm "$structures" 

Upvotes: 2

Views: 182

Answers (1)

Sir. Hedgehog
Sir. Hedgehog

Reputation: 1300

file="/home/input.txt"
grep -Po '/.*?/.*?/' $file > filenames.txt

structures='filenames.txt'
i=1
while IFS= read "line"
 do
   gunzip -c 'ens'$line'traj.pdb.gz' >> 'output'$i'.pdb'
   i=$(expr $i + 1)
 done <$structures

rm $structures

couple of logical mistakes, the counter has to be fined as one out of the while loop and the counter +1 should be inside the loop, also for the counter to work you have to use expr, in this case i made the counter start from 1, so the first entry will get this value. Also on the parameter for the while loop i dont really understand what you are doing, if it works as you have it then cool or else use a test statement after while and before the parameters.

Upvotes: 1

Related Questions