2

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" 
4
  • 4
    May be you meant to put i=$((i+1)) inside the loop? Commented Apr 25, 2016 at 20:32
  • 1
    Yes, you're right. Commented Apr 25, 2016 at 20:38
  • 4
    @EA00 Remember to double quote $line or else if it contains spaces your code will break: gunzip -c 'ens'"$line"'traj.pdb.gz' >> ... or: gunzip -c "ens${line}traj.pdb.gz" >> ... Commented Apr 25, 2016 at 20:45
  • 1
    @andlrc Thanks for the tip! Commented Apr 25, 2016 at 20:52

1 Answer 1

1
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.

Sign up to request clarification or add additional context in comments.

7 Comments

The temporary file and the spurious variables should be fixed as well. Simply grep -Po regex /home/input.txt | while IFS= read ...
You cannot have a space before the equals sign in IFS=
Also as already pointed out in comments, the silly quoting doesn't do anything useful, and the quotes are missing where they would be required (or at the very least useful).
@tripleee does the while loop actually works with the parameters defined like this? cause depending on what he i checking there i think he needs the test thought
@hedgehog - In the original code when I moved the counter inside the while loop and fixed the quotes, it seemed to work fine. But nonetheless I really appreciate all the input and as a beginner I can see how your code is more logically sound. Thanks!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.