Visit Our UNIX and Linux User Community


How to define two variable in foreach command??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to define two variable in foreach command??
# 1  
Old 08-18-2002
Question 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
d3
d4
d5
------------------------------------------------------

I have a LIST which containe the new naming "more new_name" and I have this

omd1
omd2
omd3
omd4
omd5

I tried to do crazy way to done it :-

# ls mydirectory >> old_name
# foreach file (`more old_name`) file2 (`more new_name`)
mv $file $file2
end

I know this is crazy but I don't know how to do it Smilie

Please do help me Smilie
# 2  
Old 08-19-2002
MySQL

hi,
i would suggest a different approach.

list the names of all the files in a file

$ ls > outfile

now the file new_name is expected to contain 1000 names
so paste the two files

$paste outfile new_name > newfile

the newfile will contain

d1 omd1
d2 omd2
d3 omd3
... and so on

now open the file and prepend "mv" to the beginning of each line

$vi newfile

:%s/^/mv /g

don't forget the space
now save the file
and say
$sh newfile

and bingo...

hope it helps

asif
# 3  
Old 08-19-2002
Asif,

:%s/^/mv /g

Here can u explain what %s signifies.. ^ denotes the beginning.. and what is this g for?? it denotes all the lines is it??

TIA,
Nisha
# 4  
Old 08-19-2002
MySQL

:%s/^/mv /g

this is a command mode operation of the vi editor.

open any file in the vi editor
press ESC once and type :

you will get this prompt on the bottom left of your screen

%s is for substitution.
^ is a special symbol which denotes the beginning of each line
"mv " please note there is a space after mv is the string to be substituted
and g is for replacing all the instances
g is of no use here as there will be only one beginning of the sentence in each line, but if there are multiple instances of the search string ( in this case "^"), the option g is used to replace all the instances. if not given, then only the first instance of the search string in each line will be replaced.

asif
# 5  
Old 08-19-2002
sorry
i wrote more than what you asked.
didn't read your mail carefully
# 6  
Old 08-19-2002
Re: How to define two variable in foreach command??

Quote:
Originally posted by geoquest
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
d3
d4
d5
------------------------------------------------------

I have a LIST which containe the new naming "more new_name" and I have this

omd1
omd2
omd3
omd4
omd5

I tried to do crazy way to done it :-

# ls mydirectory >> old_name
# foreach file (`more old_name`) file2 (`more new_name`)
mv $file $file2
end

I know this is crazy but I don't know how to do it Smilie

Please do help me Smilie

well from what your example says why dont you just do it this way.

#! /bin/ksh
cd (path to your directory here)
for i in `ls`; do
mv $i om$i
done

but then again. maby im not understanding your question.
# 7  
Old 08-19-2002
%s is substitution across all rows in the file.

You could use '1,$ s' for the same (1 is first $ is last) - which is the same - but you also can do '1,10 s' which is rows 1 to 10.....etc.

Previous Thread | Next Thread
Test Your Knowledge in Computers #784
Difficulty: Easy
The firmware for the TRS-80 Model 100 was developed by Steve Jobs.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to prompt user to define a variable?

Hi everyone, Is it possible to define blank vaianbles and prompt user to fill them during the script execution? A very simple example: INPUT= OUTPUT= cut -f1-4 $INPUT | sed 's/hello/goodbye/g' | sort -uV > $OUTPUTThank you in advance! Best wishes (2 Replies)
Discussion started by: lsantome
2 Replies

2. Shell Programming and Scripting

Define variable from file.

HI I have file A.txt _1A _2A _3A _4A I want define all as different variable. $1A=_1A $2B=_2A $3C=_3A $4D=_4A Now i can use any variable in my script. (3 Replies)
Discussion started by: pareshkp
3 Replies

3. Shell Programming and Scripting

In Perl can i define a hash with value as variable?

Hi, Is it possible in perl to have a hash defined with variables as theirs key values, like: %account = ('username' => 'boy', 'password' => $password); Thanks (1 Reply)
Discussion started by: zing_foru
1 Replies

4. Shell Programming and Scripting

question about define variable.

Hi, Unix Gurus, In our existing file, there is a script like #!/bin/sh step=${1:-0} cur_step=10 if ... My question is what's "${1:-0}" mean? I know it defines a variable but I don't know what's (1:-0) mean? :wall: Thanks in advance. (2 Replies)
Discussion started by: ken002
2 Replies

5. Shell Programming and Scripting

bash - define a variable

Hello, I would like to define a variable based on another variable: a=5 b$a=100 This does not work. What is the right way to do it? Thanks ---------- Post updated at 07:37 PM ---------- Previous update was at 07:33 PM ---------- Found my answer with the search function (did not... (0 Replies)
Discussion started by: jolecanard
0 Replies

6. Shell Programming and Scripting

foreach variable used in awk (please help!!)

I have a list of markers, say marker.txt: rs913257 rs1018390 rs764180 and I need to know which ROW that each marker appears in another map file, say map.txt: a rs12354060 b rs913257 c rs6650104 d rs2185539 e rs6681105 f rs1018390 g rs764180 h rs12564807 i rs3094315 The result... (4 Replies)
Discussion started by: Zoho
4 Replies

7. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

8. UNIX for Dummies Questions & Answers

define length of variable

I have a variable with a value of "05". When I add one to that variable, using the command: CURR_YY=`expr $CURR_YY + 1`, I get the value of "6", losing the leading zero (which is needed for passing to another script). How do I keep the leading zero? Thank you! (10 Replies)
Discussion started by: cbarker
10 Replies

9. UNIX for Dummies Questions & Answers

Using Grep to Define a Variable

I am using korn shell unix. I have a script that I am working with to do a check for me using a text file. #finds "Time" from the text file and cuts the second field from the #line A= grep Time test.txt | cut -f2 # the "#Missing" is being pulled from the second field of the text... (1 Reply)
Discussion started by: cspcspcsp
1 Replies

Featured Tech Videos