The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Define an alias with an embeded awk command ?? jfortes Shell Programming and Scripting 5 06-05-2007 01:03 PM
Export command giving Variable Name vs the Value set for the Variable ParNone UNIX for Dummies Questions & Answers 2 04-03-2006 11:43 AM
define length of variable cbarker UNIX for Dummies Questions & Answers 10 04-01-2005 12:42 PM
foreach command ?! geoquest UNIX for Dummies Questions & Answers 5 05-20-2002 05:11 PM
Using Grep to Define a Variable cspcspcsp UNIX for Dummies Questions & Answers 1 10-12-2001 12:22 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-18-2002
geoquest geoquest is offline
Registered User
  
 

Join Date: Apr 2002
Location: oman
Posts: 25
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

Please do help me
  #2 (permalink)  
Old 08-19-2002
asifraj's Avatar
asifraj asifraj is offline
Registered User
  
 

Join Date: Aug 2002
Location: India
Posts: 48
Thumbs up

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 (permalink)  
Old 08-19-2002
Nisha Nisha is offline
Registered User
  
 

Join Date: Jun 2002
Location: Chennai, India
Posts: 110
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 (permalink)  
Old 08-19-2002
asifraj's Avatar
asifraj asifraj is offline
Registered User
  
 

Join Date: Aug 2002
Location: India
Posts: 48
Thumbs up

:%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 (permalink)  
Old 08-19-2002
asifraj's Avatar
asifraj asifraj is offline
Registered User
  
 

Join Date: Aug 2002
Location: India
Posts: 48
sorry
i wrote more than what you asked.
didn't read your mail carefully
  #6 (permalink)  
Old 08-19-2002
Optimus_P Optimus_P is offline Forum Advisor  
flim flam flamma jamma
  
 

Join Date: May 2001
Location: Chicago IL, USA
Posts: 1,006
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

Please do help me

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 (permalink)  
Old 08-19-2002
peter.herlihy peter.herlihy is offline
Registered User
  
 

Join Date: Nov 2001
Location: New Zealand
Posts: 333
%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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:16 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0