Accessing Multiple files using for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Accessing Multiple files using for loop
# 1  
Old 06-03-2010
Question Accessing Multiple files using for loop

Hi All,

I have some files in my directory, and i want to pull all data using for loop....I am using following code but getting error..!

Code:
for file in {file1, file2, file3, ..... filen}
do
 L="$(tail -1 $file)";NUM=${L%%|*};DAT=${L##*|}
 echo $NUM>>filedata.txt
done

Error:
Code:
tail: cannot open input
tail: cannot open input


i tried with other codes, some gave blank data and some something else.....but can understand where the prob is.... Smilie

please help
# 2  
Old 06-03-2010
Try:

Code:
for file in file1 file2 .. filen
[...]

Or:

Code:
for file in <glob pattern>
[...]

For example, if your files are named file1, file2 ..:

Code:
for file in file*
[...]


It's a good practice to quote your variables too:

Code:
tail -1 "$file"

# 3  
Old 06-03-2010
try with:

Code:
for file in "file1" "file2" "file3" ... "filen"

# 4  
Old 06-07-2010
MySQL

Thanks a lot........now its working for me...!!

i used file in "file1" 'file2".......!


NB:Please let me know how can i close threads if my issue gets resolved.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output Multiple Files in the For Loop

while IFS= read -r line do # sV for version detection nmap -T4 -Pn -v -sS "$line" > "text/$line" done < <(grep '' $file) Hi, where line represents the IP. I am using NMAP to do scanning. How can I set to execute that command in the loop several concurrently at a time instead of one... (5 Replies)
Discussion started by: alvinoo
5 Replies

2. Shell Programming and Scripting

Loop through multiple files in bash script

Hi Everybody, I'm a newbie to shell scripting, and I'd appreciate some help. I have a bunch of .txt files that have some unwanted content. I want to remove lines 1-3 and 1028-1098. #!/bin/bash for '*.txt' in <path to folder> do sed '1,3 d' "$f"; sed '1028,1098 d' "$f"; done I... (2 Replies)
Discussion started by: BabyNuke
2 Replies

3. Shell Programming and Scripting

Accessing multiple directories in loop

Hi Guys, I need to access multiple directories whcih is following similar structure and need to copy those files in desitination path. for eg : if ] then cd ${DIR}/Mon/loaded echo "copying files to $GRS_DIR" cp * ${DIR}/Mon/ echo "Files of Monday are Copied" fi if ] then... (5 Replies)
Discussion started by: rohit_shinez
5 Replies

4. Shell Programming and Scripting

While loop for multiple files

Hi I need to loop until some files are not there .If lck file is there I need to sleep.until its not there I am writing a code like this while && && do echo "Lck file exits" sleep 1 done But this one is not working.Can some one help here please (1 Reply)
Discussion started by: ginrkf
1 Replies

5. 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

6. 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

7. Shell Programming and Scripting

accessing variable from while loop

Hi all, Here is an outline of the problem: #variable declared at start of script x=0; #a function that increments x by 1 every 10 seconds incrementX(){ increments x every 10 seconds; } #i want this to output the value of x every second. The problem is that x is always reported... (3 Replies)
Discussion started by: free2rhyme2k
3 Replies

8. Shell Programming and Scripting

Read and edit multiple files using a while loop

Hi all, I would like to simply read a file which lists a number of pathnames and files, then search and replace key strings using a few vi commands: :1,$s/search_str/replace_str/g<return> but I am not sure how to automate the <return> of these vis commands when I am putting this in a... (8 Replies)
Discussion started by: cyberfrog
8 Replies

9. Shell Programming and Scripting

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... (6 Replies)
Discussion started by: DeuceLee
6 Replies

10. Shell Programming and Scripting

Sed inside bash script - accessing multiple files

I have a file (email) containing email addresses. I have a second file (terms) that contains simple regular expressions and words/characters. Here are some examples: \.trainee \.group \.web I want to go through email and delete lines containing the expressions/words from terms and write... (1 Reply)
Discussion started by: manouche
1 Replies
Login or Register to Ask a Question