LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Parameter substitution in loops (https://www.linuxquestions.org/questions/linux-newbie-8/parameter-substitution-in-loops-4175506818/)

Jeff9 06-02-2014 05:24 PM

Parameter substitution in loops
 
I'm trying the following loop to process a bunch of files and create a new filename for the output file, but it's not working. Also, I want to make the new value for TT to be five digits, with a leading zero.

Basically, I need to extract every 10 thousandth line from a bunch of input files. In each output file, I want to include a 5 digit line number in each output file's filename (so I know where it came from).

My current code just hangs (on further inspection, it created a whole bunch of strangely named files which I now must get rid of).

Code:

for ((TT=10000; TT<=100000; TT+=10000))
do
        for fn in *U.csv;
        do
                prefix=${fn%tol*}
                suffix="t=$TT_forward.LC.csv"
                nfn=$prefix$suffix
                echo "$nfn"

                echo head -n "$TT" "$fn" | tail -n 1 >"../$nfn"
        done
done


Jeff9 06-02-2014 05:41 PM

Actually, I have it almost working (so there!, all you experts). I still need a five digit number in the first iteration.

Code:

for ((TT=9999; TT<30000; TT+=10000))
do
        for fn in N=0008_Beta=0[456]*.csv;
        do
                prefix=${fn%tol*}
                suffix="t=${TT}_forward.LC.csv"
                nfn=$prefix$suffix
                echo $prefix
                echo $suffix
                echo $nfn

                echo "head -n '${TT}' '$fn' | tail -n 1 >../'$nfn'"
        done
done


Jeff9 06-02-2014 10:52 PM

On third thought, nevermind. It seems I need the 10,000th line and every 10 thousand after that. So I don't even have to pad the zeros.

grail 06-02-2014 11:46 PM

I would probably use awk with NR%10000, then you need neither loop (as awk will handle it) and then just process the files as required.


All times are GMT -5. The time now is 04:35 PM.