Writing a for loop that processes multiple input files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Writing a for loop that processes multiple input files
# 1  
Old 09-27-2011
Writing a for loop that processes multiple input files

I would like to write a for loop that does the following:

I have a file called X.txt and other files called 1.txt,2.txt, .....,1000.txt.

I want to substitute the 6th column of the file X.txt with 1.txt and store the output as X.1. Then I want to do the same with X.txt and 2.txt and store the output as X.2 and so on all the way up to 1000.txt.

How do I write a for loop that achieves that? Thank you!Smilie
# 2  
Old 09-27-2011
Like this?
Code:
#!/bin/sh

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

exit 0

It replaces the content of the sixth column on X.txt with the name `<number>.txt` and writes to X.<number>

Or this:
Code:
#!/bin/sh

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

exit 0

This one replaces the sixth column of X.txt with the content of the sixth column of `<number>.txt` and writes to X.<number>

Last edited by tukuyomi; 09-27-2011 at 04:00 PM.. Reason: Missing `exit 0`
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Detail on For loop for multiple file input and bash variable usage

Dear mentors, I just need little explanation regarding for loop to give input to awk script for file in `ls *.txt |sort -t"_" -k2n,2`; do awk script $file done which sorts file in order, and will input one after another file in order to awk script suppose if I have to input 2 or... (4 Replies)
Discussion started by: Akshay Hegde
4 Replies

2. Shell Programming and Scripting

FOR loop with multiple files as input and awk

Hi all , i want to pass multiple files as input to a for loop for i in file1 file2 file3 do some awk action < $i >> $i.out done but im getting error in that for loop is the way i use to pass files to awk using for correct and 2.we can directly pass multiple files to awk as... (7 Replies)
Discussion started by: zozoo
7 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Writing a loop to changing the names of files in a directory

Hi, I would like to write a loop to change the names of files in a directory. The files are called data1.txt through data1000.txt. I'd like to change their names to a1.txt through a1000.txt. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

4. UNIX for Dummies Questions & Answers

Writing a for loop to manipulate multiple files

Hi, I have 1000 text files in a folder that are labeled data1.txt all the way to data1000.txt. I want to write a small script that manipulates the text files in this way: (1) cut the 2nd and 9th columns of the text files (2) sort by the numerical value in the 9th column (3) then save the rows... (3 Replies)
Discussion started by: evelibertine
3 Replies

5. Shell Programming and Scripting

awk, multiple files input and multiple files output

Hi! I'm new in awk and I need some help. I have a folder with a lot of files and I need that awk do something in each file and print a new file with the output. The input file name should be modified when I print the outpu files. Thanks in advance for help! :-) ciao (5 Replies)
Discussion started by: gabrysfe
5 Replies

6. UNIX for Dummies Questions & Answers

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 || awk... (4 Replies)
Discussion started by: evelibertine
4 Replies

7. UNIX for Dummies Questions & Answers

Writing a loop to merge multiple files by common column

I have 100 data files labelled 250.1.txt through 250.100.txt. The second column of the data files partially match (there is about %90 overlap). Each data file has 4 columns. I want the merge all these text files by the matching values in the second column. In the output, the first column should... (1 Reply)
Discussion started by: evelibertine
1 Replies

8. Shell Programming and Scripting

Writing a Perl Script that processes multiple files

I want to write a Perl script that manipulates multiple files. In the directory, I have files 250.*chr$.ped where * is from 1 to 1000 and $ is from 1-22 for a total of 22 x 10,000 = 22,000 files. I want to write a script that only manipulates files 250.1chr*.ped where * is from 1 to 22.... (10 Replies)
Discussion started by: evelibertine
10 Replies

9. UNIX for Dummies Questions & Answers

Writing a loop to manipulate a script and store it in multiple output files

I have a script where the the 9th line looks like this: $filename=sprintf("250.1chr%d.ped", $N); I want to modify this script 1000 times, changing 250.1chr%d.ped to 250.2chr%d.ped, 250.3chr%.ped.......and so on all the way to 250.1000chr%d.ped and store each output in files called ... (4 Replies)
Discussion started by: evelibertine
4 Replies

10. Shell Programming and Scripting

Multiple processes writing on the same file simultaneously

Hi All, I have encountered a problem,please help me. I have a script in which multiple processes are writing on to the same file . How should I stop this , I mean lock mechanism can be implemented or we can write the at different files and then concatenate the files. What would be a better... (1 Reply)
Discussion started by: Sayantan
1 Replies
Login or Register to Ask a Question