LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-02-2014, 06:24 PM   #1
Jeff9
Member
 
Registered: Jun 2013
Posts: 36

Rep: Reputation: Disabled
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

Last edited by Jeff9; 06-02-2014 at 06:30 PM.
 
Old 06-02-2014, 06:41 PM   #2
Jeff9
Member
 
Registered: Jun 2013
Posts: 36

Original Poster
Rep: Reputation: Disabled
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

Last edited by Jeff9; 06-02-2014 at 06:43 PM.
 
Old 06-02-2014, 11:52 PM   #3
Jeff9
Member
 
Registered: Jun 2013
Posts: 36

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-03-2014, 12:46 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash parameter substitution question kushalkoolwal Programming 5 03-29-2011 09:36 PM
shell getopts: opt w/ optional parameter is taking next opt as its parameter! GrapefruiTgirl Programming 22 10-27-2010 07:00 AM
Help Requested with bash Parameter Substitution haleba Linux - Software 3 08-23-2010 02:37 PM
substitution ovince Programming 3 04-28-2007 06:35 AM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 05:54 AM


All times are GMT -5. The time now is 12:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration