Visit Our UNIX and Linux User Community


foreach command ?!


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers foreach command ?!
# 1  
Old 05-20-2002
foreach command ?!

SaLAam

What is the best way to change a word withing a files name. I know I'm not clear enough I will give example : -


I have in /test/test N number of files like this

1662_WAITING
1666_WAITING
1670_DONE
1678_DONE
1663_WAITING
1667_WAITING
1673_WAITING
1679_WAITING
1664_WAITING
1668_WAITING
1676_TAPE_ERROR
1683_DONE
1665_WAITING
1669_DONE
1677_TAPE_ERROR
1685_DONE

I want to change the word "####_WAITING" to "####_DONE" or for all the words after the _ at once if possiple. I tried foreach command and I mange to get something like "####_WAITING_DONE" but this is not what I want :-(.

thanks in advance

Abdulkarim
# 2  
Old 05-20-2002
Hi Abdulkarim

Try this:
Code:
for i in `ls /test/test`
do
   echo $i | grep "WAITING" >/dev/null
   if [ $? -eq 0 ]
   then
      j=`echo $i | sed "s/WAITING/DONE/"`
      mv /test/test/$i /test/test/$j
   fi
done

Let me know how you get on.

Cheers
Helen Smilie

added code tags for readability --oombera

Last edited by oombera; 02-19-2004 at 11:31 AM..
# 3  
Old 05-20-2002
do command not found!!

hi helen,

Thanks for your help, but when I tried your command it's not working saying do command not found!!

Maybe I forget to tell you I'm using unix sun 5.6.
and I don't not know what is "fi" for in ur script.I want to know if possiple why u direct the output to /dev/null ??? I'm still a very new unix user :-D.


thanks
Abdulkarim
# 4  
Old 05-20-2002
Try with sed

man sed.

Regards. Hugo.
# 5  
Old 05-20-2002
Hi Abdulkarim

I see, I'm using HP-UX 11.00 with a Korn Shell. I'm not sure how Sun 5.6 differs. What shell are you using?

I must admit I have not heard of the 'foreach' statement. Are you doing a shell script or using a programming language?

'fi' is the end of the 'if' statement. If statement syntax is as follows:

if <test>
then
<list of commants>
fi

I have redirected output to /dev/null because I don't want the result of the echo statement to appear on the screen. I just want to know the return value of the statement. If you don't already know, anything that goes to /dev/null 'disappears'. Its a bit like a waste bin.

Cheers
Helen
# 6  
Old 05-20-2002
The foreach is in csh.

He is probably running the script the same way (which is why 'do' isn't found). Solaris differs greatly to HP but the shells should work pretty much the same.

Your script, Helen, does work on under Solaris 5.6 in ksh.
thehoghunter
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #706
Difficulty: Medium
Shared information bias is known as the tendency for group members to spend more time and energy discussing information that all members are already familiar with (i.e., shared information), and less time and energy discussing information that only some members are aware of.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get following result using foreach command?

Dear all, For example, if data contains following numbers:1 2 3 4 5 6 I would like get like below using foreach command,1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 (1 Reply)
Discussion started by: Ryan Kim
1 Replies

2. Shell Programming and Scripting

Using foreach with two lists

Hi everybody, I'm trying to use a foreach command with two lists. The file.txt looks like this: var1: 100 200 300 var2: 3 6 9 I'm trying to use a foreach command to associate the two variables together. My script looks like this: #! /bin/tcsh set a=(`cat file.txt | grep 'var1' | cut -d... (8 Replies)
Discussion started by: SimonWhite
8 Replies

3. Shell Programming and Scripting

foreach in csh

I have a foreach loop in a csh script and I noticed that it tries to find the files with the pattern *$searchpt* in the file name. I'm confused as I never specified checking for the files. foreach f ( *$searchpt* ) set fnew = `echo $f | awk -v searchpat=$searchpt \ ... (1 Reply)
Discussion started by: kristinu
1 Replies

4. Shell Programming and Scripting

Learning foreach

im newbie at shell scripting. why do the following code #!/bin/tcsh setenv CBC ~/cbc/models/ foreach mix (p00p00 p02p00 p02p04) echo $mix cp $CBC/*$mix*Gyr*fits $mix/ end print(copy) only the first mix? % ./copyfromcbc.sh p00p00 wasn't it supposed to run through all words... (0 Replies)
Discussion started by: prtc
0 Replies

5. UNIX for Dummies Questions & Answers

foreach question

OK, so I am extremely rusty and am just getting back to Unix after 9 years. I'm stuck on something easy. I want to search line-by-line for a string in a file, and I want to do this to a series of files in a directory. This works fine to do the search: while read i; do grep $i file2; done... (3 Replies)
Discussion started by: moldoverb
3 Replies

6. Shell Programming and Scripting

foreach loop

Hi Guys, I have a loop which uses a wildcard i.e. foreach f (*) but when I execute the tcsh file in unix then it gives me an error ->>>>>>>foreach: words not parenthesized<<<<<<<<<<- Any help. (1 Reply)
Discussion started by: abch624
1 Replies

7. Shell Programming and Scripting

Foreach loop

What am I doing wrong with this foreach loop? foreach var ($argv) @sum = $sum + $var (4 Replies)
Discussion started by: haze21
4 Replies

8. Shell Programming and Scripting

foreach/grep help!

#!/bin/bash foreach x (67402996 67402998) { grep -a x FINAL2006.dat >> MISSING_RECORDS.dat } I'm trying to pass a list to the variable x, and then grep for that string in FINAL2006.dat... Final2006.dat is in the same folder as my .sh file. I call this with a .cmd file... At any rate,... (6 Replies)
Discussion started by: JimWork
6 Replies

9. Shell Programming and Scripting

How to define two variable in foreach command??

Hello, I just want to know how If it's possiple to define 2 variable using foreach command ??? I have directory inside that directory around 1000 file, I want to rename all of this files to something I have it in a list. Example :- ------This is what in my directory---------- d1 d2... (14 Replies)
Discussion started by: geoquest
14 Replies

Featured Tech Videos