for loop, calling and editing multiple files inside


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting for loop, calling and editing multiple files inside
# 1  
Old 04-14-2009
for loop, calling and editing multiple files inside

hey guys,

I'm trying to call and modify multiple files inside the for loop, i can't get it to work...

------------------------

AFILE=/dir/a_file.txt
BFILE=/dir/b_file.txt
CFILE=/dir/c_file.txt

ADESTFILE=/dir/a_dest_file.txt
BDESTFILE=/dir/b_dest_file.txt
CDESTFILE=/dir/c_dest_file.txt

AFINFILE=/dir/a_final_file.txt
BFINFILE=/dir/b_final_file.txt
CFINFILE=/dir/c_final_file.txt

for i in A B C
do
grep "something" $iFILE > $iDESTFILE
'do something with' $iFINFILE
done

-------------------------------

So as you guys can see, if I had A through Z, I don't want to write out everything in the loop 26 times.

Any ideas?

I tried '$"$i"FILE' but that didn't work. I just want to replace the $iFILE with AFILE and have it reference that file instead of the literal AFILE text.

thanks in advance.
# 2  
Old 04-14-2009
are you going to define all your files (from A to Z) inside the script?? ie hardcoded? or are you going to store them in a file or something. then open the file and loop through them.?
# 3  
Old 04-14-2009
Your problem is the shells way of parsing its input and what you have encountered is not a bug, its a feature.

You might want to read this thread where i have explained to problem and its solution in detail.

Still, in your case you would not need all the variables, because you could simply use:

Code:
for i in A B C ; do
     do_something /dir/${i}_file.txt > /dir/${i}_dest_file.txt
done

I hope this helps.

bakunin
# 4  
Old 04-14-2009
Quote:
Originally Posted by DeuceLee
hey guys,

I'm trying to call and modify multiple files inside the for loop, i can't get it to work...

------------------------

AFILE=/dir/a_file.txt
BFILE=/dir/b_file.txt
CFILE=/dir/c_file.txt

ADESTFILE=/dir/a_dest_file.txt
BDESTFILE=/dir/b_dest_file.txt
CDESTFILE=/dir/c_dest_file.txt

AFINFILE=/dir/a_final_file.txt
BFINFILE=/dir/b_final_file.txt
CFINFILE=/dir/c_final_file.txt

Code:
for i in A B C
do
    grep "something" $iFILE > $iDESTFILE


The shell is interpreting iFILE as a variable name; enclose the variable name in braces to remove the ambiguity and eval for the indirection:

Code:
eval "grep 'something' \"\$${i}FILE\" > \"\$${i}DESTFILE\""

Quote:
Code:
    'do something with' $iFINFILE
done

-------------------------------

So as you guys can see, if I had A through Z, I don't want to write out everything in the loop 26 times.

Any ideas?

I tried '$"$i"FILE' but that didn't work. I just want to replace the $iFILE with AFILE and have it reference that file instead of the literal AFILE text.

thanks in advance.
# 5  
Old 04-14-2009
thank you ghostdog, bakunin and cfajohnson...between the three of you, i've got it taken cared of...

i'm going to take ghostdog and bakunin's suggestion of referenceing the file directly...i forgot to mention that there's a set of variables that is not as pretty looking like a_file.txt, b_file.txt, etc..., for that case, i'm going to use bakunin and cfa's eval statement...

thanks a bunch guys!
# 6  
Old 04-14-2009
crap...i thought i had it but i forgot to add a small detail...please see if you guys can help me with this...

this relates to cfa's statement:

eval "grep 'something' \"\$${i}FILE\" > \"\$${i}DESTFILE\""

I'm trying to add these awk statements in the middle:

eval "grep 'something' \"\$${i}FILE\" | awk -F"=" '{print$2}' | awk -F" " '{print $1}' > \"\$${i}DESTFILE\""

it's not working for me...thanks
# 7  
Old 04-14-2009

Don't try to cram everything into a single line. Generate the filenames first, then use a normal command (and you don't need awk).

Code:
eval "IFILE=\$${i}FILE DFILE=\$${i}DESTFILE"
result=$( grep 'something' "$IFILE" )
result=${result#*=}
printf "%s\n" "${result%% *}" > "$DFILE"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with copying files into dir inside for loop

Hi , I'm trying to move/copy the files inside the loop into a directory . I tried the below code and the issue is the data is not copying into the created directory but the files are copying into another file file_path="/home/etc" Last_Day=20130930 mkdir $file_path/ARC_${Last_Day} ... (3 Replies)
Discussion started by: smile689
3 Replies

2. Shell Programming and Scripting

Avoiding some files inside a loop

In my script I need to loop around some files like below example files are fa.info.abcd fa.info.bxde fa.info.cdas ------ for test_data in fa.info.* do # Some text appending logic applied # Copy to another directory done Now I need to discard some files while looping around ... (9 Replies)
Discussion started by: smile689
9 Replies

3. Shell Programming and Scripting

For loop inside awk to read and print contents of files

Hello, I have a set of files Xfile0001 - Xfile0021, and the content of this files (one at a time) needs to be printed between some line (lines start with word "Generated") that I am extracting from another file called file7.txt and all the output goes into output.txt. First I tried creating a for... (5 Replies)
Discussion started by: jaldo0805
5 Replies

4. UNIX for Dummies Questions & Answers

Editing multiple files with vi, why :p is not working

Hi, Can any gurus advise why :p is not working on my vi? When editing multiple files, :n works and it takes me the next file, but :p which is supposed to take me back to the previous file does not work. Please advise. Thanks in advance. (2 Replies)
Discussion started by: newbie_01
2 Replies

5. Shell Programming and Scripting

Editing all files inside folders and updating pos 88-94 with continuous numbering.

Here is expected output:- For each file with following file name pattern we need to look at position 1 inside first file matching our search criteria if first letter of the line is 5 then position 88-94 will be 0000001 then look for line immediately after 5 which starts with i.e. position 1 = 8... (6 Replies)
Discussion started by: lancesunny
6 Replies

6. Shell Programming and Scripting

need help to compare 2 files inside a loop

Basically, I have a user input a colour into a variable, and then i echo that variable into a text file. Then I need to compare those 2 files which is easy using the diff command. The thing I can not get is how to do this inside of a loop until the variable matches the file. Also if the user enters... (6 Replies)
Discussion started by: cstadnyk1
6 Replies

7. Shell Programming and Scripting

editing names of files in multiple folder

I have 1000's of directories which is named as numbers. Each directory contains multiple files. Each of these directories have a file named "att". I need to rename all the att files by adding the directory name followed by "_" then att for each of the directories. Directories 120 att... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

8. Shell Programming and Scripting

How to make an editing script work for multiple files?

Hey everybody, I have a script for making a string substitution in a file. I am trying to modify it in order to make the same modifcation to multiples files. here is what I have so far. #!/bin/csh set p1="$1" shift set p2="$1" shift foreach x ($*) if ( { grep -w -c "$p1" $x } ) then mv... (7 Replies)
Discussion started by: iwatk003
7 Replies

9. Shell Programming and Scripting

Rename contents inside multiple files

Hi, I have 100 files in a directory. Each file have the following format >CtbRe01234 fdfjdhfkdfkd >CtL2B0456 gjfgfkgjfkgjfk >CmdrE05768 fghdjskksllfkLike this I have many files in the directory. What I want is; rename the header content in each file such that the above file... (6 Replies)
Discussion started by: Lucky Ali
6 Replies

10. UNIX for Dummies Questions & Answers

Editing one string in multiple files

I am trying to edit multiple files from one directory and including all the files in all the sub directories. My string opens each file, puts the text on my screen and does not save the new information to the file. I am using a variable in my script, and wondering if that is what is choking it. ... (1 Reply)
Discussion started by: Skoshi
1 Replies
Login or Register to Ask a Question