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 > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
foreach loop ROOZ Shell Programming and Scripting 3 06-05-2008 03:20 PM
foreach loop + 2 variables JamesGoh Shell Programming and Scripting 3 06-04-2008 03:36 AM
using foreach loop sam4now Shell Programming and Scripting 6 04-26-2008 11:22 AM
foreach loop abch624 Shell Programming and Scripting 1 03-19-2008 09:34 PM
Foreach loop haze21 Shell Programming and Scripting 4 11-16-2007 03:11 AM

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 12-21-2005
clipski clipski is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 4
foreach loop question

Hello, I am new at this forum so please bare with me on this.

Within a given directory, I have a list of files in which in each file, I would like to do a substitution. I would like to substitute the string mlcl to mll in each file using the foreach command. I dont quite get how to do that. If you can provide help, it would surely be appreciated
  #2 (permalink)  
Old 12-21-2005
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
Are you talking about perl?
  #3 (permalink)  
Old 12-21-2005
clipski clipski is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 4
I am in the Unix environment at a directory and thought to do something like

set x = `ls`
foreach i ($x)
`sed s/mlcl/mll/g`
end

but this doesnt seem to work
  #4 (permalink)  
Old 12-21-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Do you want to move the files too (this is sh/ksh/bash)?

Code:
$ touch mlcl_1 mlcl_2 mlcl_3
$ ls mlcl_*
mlcl_1  mlcl_2  mlcl_3
$ ls | while read file; do
>   mv $file `echo "${file}" | sed 's/mlcl/mll/'`
> done
$ ls
mll_1  mll_2  mll_3
$
Cheers
ZB
  #5 (permalink)  
Old 12-21-2005
clipski clipski is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 4
I want to keep the change in the same file name, so I think
that I just dont need to do the file name touches and the mv
command. Is that corret Zazzy?
  #6 (permalink)  
Old 12-21-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
My above post was an interactive session, hence the "touch"es.

If you don't want to keep the changes, you can easily do (as long as there aren't a load of files....)
Code:
ls *mlcl* | sed 's/mlcl/mll/g'
Else; just do
Code:
ls | while read file; do
  echo "${file}" | sed 's/mlcl/mll/'
done
If you want to keep the filename change, just add in the mv.

Cheers
ZB
  #7 (permalink)  
Old 12-22-2005
mahendramahendr mahendramahendr is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2005
Location: London
Posts: 222
why you have put sed command in ` quotes ?? i guess it is not required

foreach i ( * )
sed s/mlcl/mll/g
end
Sponsored Links
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 10:56 AM.


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