How to loop this process?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to loop this process?
# 1  
Old 11-14-2010
How to loop this process?

for two txt files, f1 and f2, I like to do the following
Code:
grep "abcde" f1 > abcde$f1
grep "xyz" f1 > xyz$f1

can I use a loop to get this done? Thanks
Code:
 
for i in f1 f2
do
grep "abcde" $i > abcde$i
grep "xyz" $i > xyz$i
...
done


Last edited by Scott; 11-14-2010 at 02:59 PM.. Reason: Code tags, please...
# 2  
Old 11-14-2010
Good, but your loops are not equivalent. Use a constant separator in you output, like $f1.abcde. Having the file name first is more intuitive, too.
This User Gave Thanks to DGPickett For This Post:
# 3  
Old 11-14-2010
Thank you for suggestion!

Additional question

I like to loop a list of files which named file1, file2, file3, file4, etc

if I like to loop them all over
Code:
for f in file1, file2, file3, file4
do 
echo "processing" $f
done

how to use a regular expression to loop file$i instead?
Code:
for  i in (1:5) 
do
echo "processing file" $i
done

would this work? thanks

Last edited by Scott; 11-14-2010 at 03:00 PM.. Reason: Code tags
# 4  
Old 11-14-2010
bash solution
Code:
for i in {1..5}
do 
   echo "processing file" $i
done

This User Gave Thanks to danmero For This Post:
# 5  
Old 11-14-2010
And you can use this bash/ksh Smilie
Code:
for i in $(seq 1 5)
do
echo "processing" $i
done

This User Gave Thanks to ygemici For This Post:
# 6  
Old 11-14-2010
seq is not a shell keyword Smilie
# 7  
Old 11-15-2010
yes seq is not shell builtin or keyword..
seq is an utility that portion of gnu coreutils package Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Process files in loop which have spaces in name

I have a folder with files and I have to process them in a loop. However the filenames have space characters, so the list get split. $ touch "File Number_1" $ touch "File Number_2" $ ls "/tmp/File Number"_* /tmp/File Number_1 /tmp/File Number_2 I tried following (sorry for using the... (3 Replies)
Discussion started by: Wernfried
3 Replies

2. Shell Programming and Scripting

Script to loop process

As I would like to test the open files usage , I would like to have a process that use the open files up to a certain amount eg. 1000 . If I want to have a script ( may be run in a loop ) that could repeatly use open files resource , so that the usage of open files increases , may I know how to... (10 Replies)
Discussion started by: ust
10 Replies

3. UNIX for Dummies Questions & Answers

For loop in process each file

Hi I have following codecd /tmp/test/ for vfile in `ls -1` do for vlink in `ls -l /tmp/testfile/*|bin/grep "local/init\.d/$vfile$"|bin/awk -F"->" '{print($1)}'|bin/awk -F"/" '{print($NF)}'` I know `ls -1` list only file, but I don't... (3 Replies)
Discussion started by: stew
3 Replies

4. UNIX for Dummies Questions & Answers

Timed loop to scan for a process

Hi all, Looking for some help, I have written a very simple script to pass to PowerHA to act as an indicator to activate failover required. Where i get completely lost is I have to create a wait and carry out the grep for the process once every 10 seconds this works but need to embed this... (1 Reply)
Discussion started by: Gary Hay
1 Replies

5. Shell Programming and Scripting

need to process for loop faster

I have the following code running against a file. The file can have upwards of 10000 lines. problem is, the for loop takes a while to go through all those lines. is there a faster way to go about it? for line in `grep -P "${MONTH} ${DAY}," file | ${AWK} -F" " '{print $4}' | awk -F":"... (2 Replies)
Discussion started by: SkySmart
2 Replies

6. Shell Programming and Scripting

Loop to process 2 files with same name in different path

Hello forum members, I hope you can help me with this I don't know hot to reach. I have a list of files in "/home/MyPath1/" and in "/home/MyPath2/". The files have the same name in both folders. (but different content, the content doesn't matter here I think) /home/MyPath1/ filename1.txt... (4 Replies)
Discussion started by: Ophiuchus
4 Replies

7. Shell Programming and Scripting

Process checking loop

Hi, I want to create a script who will check if the java process is running & if it finds the process is still there it continues to execute & when the process completes it exit from the script. I have written a code to check & notify the process existence but i am not getting how to write... (4 Replies)
Discussion started by: d8011
4 Replies

8. Shell Programming and Scripting

loop when process running

Hi Gurus, Could you please help me to create a shell script that will be started by a cron job once every night at 24.00 h (that should bee easy:)) The shell script should control every 30 seconds the name of a process, and when the process doesn't run anymore it should execute a few further... (12 Replies)
Discussion started by: blackwire
12 Replies

9. Shell Programming and Scripting

Check for statup process in loop?

I've written a script to check for Oracle's listener, eventman and pmon processes however there are several databases that startup which can take several minutes. I'd like to add code to my current script that greps for the process “startup” and whether its condition is true or false. If the... (1 Reply)
Discussion started by: dataciph3r
1 Replies

10. Shell Programming and Scripting

loop of killing and calling process

I make two process killing and calling one process I want to do this repeatedly many time between the interval(sleep) What will be the command to do this, can you make as one do use sleep between and run clear the memory(sh sync.sh) I need your advice, the script will be like this killps... (1 Reply)
Discussion started by: 197oo302
1 Replies
Login or Register to Ask a Question