Writing a loop to process multiple input files by a shell script | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Writing a loop to process multiple input files by a shell script

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-11-2011
evelibertine evelibertine is offline
Registered User
 
Join Date: May 2011
Last Activity: 27 December 2013, 6:16 PM EST
Posts: 193
Thanks: 94
Thanked 0 Times in 0 Posts
Writing a loop to process multiple input files by a shell script

I have multiple input files that I want to manipulate using a shell script. The files are called 250.1 through 250.1000 but I only want the script to manipulate 250.300 through 250.1000. Before I was using the following script to manipulate the text files:

for i in 250.*; do
[ "$i" = 'X.txt' ] || awk 'NR==FNR{A[NR]=$6;next}{$6=A[FNR]}1' "$i" 'X.txt' > "X.${i%.txt}"
done

But the script above manipulates all scripts 1 through 1000 whereas I want it to specifically manipulate 300 through 1000. How can I change the script above to do that?
Sponsored Links
    #2  
Old 10-11-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 July 2014, 6:49 PM EDT
Location: Saskatchewan
Posts: 18,976
Thanks: 748
Thanked 3,145 Times in 2,951 Posts
What's your system?

What's your shell?

You've already been given a perl solution, what was wrong with it?
Sponsored Links
    #3  
Old 10-11-2011
evelibertine evelibertine is offline
Registered User
 
Join Date: May 2011
Last Activity: 27 December 2013, 6:16 PM EST
Posts: 193
Thanks: 94
Thanked 0 Times in 0 Posts
My system is Ubuntu 11.04 and shell is bash, I believe.

Well that perl script worked but the output of the bash loop that I posted above was the input of the perl script. So I need to get this bash loop working first.
    #4  
Old 10-11-2011
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 21 June 2012, 3:19 PM EDT
Location: Bahia, Brazil
Posts: 1,203
Thanks: 1
Thanked 103 Times in 100 Posts
Can this work for you:

Code:
for i in 250.[3-9]* 250.1000; do 
  [ "$i" = 'X.txt' ] || awk 'NR==FNR{A[NR]=$6;next}{$6=A[FNR]}1' "$i" 'X.txt' > "X.${i%.txt}"
done

Sponsored Links
    #5  
Old 10-11-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 July 2014, 6:49 PM EDT
Location: Saskatchewan
Posts: 18,976
Thanks: 748
Thanked 3,145 Times in 2,951 Posts
Quote:
Originally Posted by evelibertine View Post
My system is Ubuntu 11.04 and shell is bash, I believe.
If you have BASH, you have for-loop:


Code:
for ((N=300; N<=1000; N++))
do
        FILENAME=250.$N

        ...
done

This would be better than for file in * since it's less likely to match other things by accident and isn't going to ever cram too many arguments for for to handle into anything.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Writing a loop to merge multiple files by common column evelibertine UNIX for Dummies Questions & Answers 1 10-07-2011 11:20 AM
Writing a loop to manipulate a script and store it in multiple output files evelibertine UNIX for Dummies Questions & Answers 4 10-05-2011 05:40 PM
Writing a for loop that processes multiple input files evelibertine UNIX for Dummies Questions & Answers 1 09-27-2011 02:53 PM
How to process multiple input files using Shell scripting evelibertine Shell Programming and Scripting 0 09-27-2011 02:00 PM
How to Process input files from folder in shell script? Poonamol Shell Programming and Scripting 2 10-01-2010 05:03 AM



All times are GMT -4. The time now is 09:33 PM.