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
find and replace javeed7 Shell Programming and Scripting 1 04-02-2008 09:00 AM
find and replace mahabunta UNIX for Dummies Questions & Answers 7 09-21-2006 12:05 PM
find and replace valhutch UNIX for Dummies Questions & Answers 4 07-29-2006 05:20 PM
find and replace vikas_j@hotmail UNIX for Dummies Questions & Answers 3 02-25-2002 05:41 PM
Find & Replace gagansharma Shell Programming and Scripting 3 11-27-2001 04:17 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 03-03-2008
NycUnxer NycUnxer is offline
Registered User
  
 

Join Date: Sep 2007
Location: AIX-VILLE
Posts: 88
Find and Replace

After running a command like

grep -ir files2/ *

This will find all the files that contain "files2/" in it.

For example if it finds

files2/dir/today
files2/dir/yesterday
files2/dir/2daysago

Now it may find 100 instances, so is there a quick find and replace command I can use? I basically want to remove "files2" from all instances. tia.
  #2 (permalink)  
Old 03-03-2008
kamitsin's Avatar
kamitsin kamitsin is offline
Registered User
  
 

Join Date: Nov 2006
Location: /dev/null
Posts: 177
What do you want. remove file2 from filenames or from the output of the grep command ? If you want to remove file2 from the output of grep command then use 'sed'

PS - i have limited access to internet so may not be able to answer very promptly.
  #3 (permalink)  
Old 03-04-2008
timontt timontt is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 44
If you want to remove "files2/" from the file itself, vi the file and type below and enter

:%s/^files2\///g


If you want to repalce from grep output

# grep files2 filename | sed 's/files2\///'


This all will give u output
dir/today
dir/yesterday
dir/2daysago
  #4 (permalink)  
Old 03-05-2008
NycUnxer NycUnxer is offline
Registered User
  
 

Join Date: Sep 2007
Location: AIX-VILLE
Posts: 88
Ok, maybe I should describe what I want to do more clearly.

On a windows server where we ftp files to, we have unix scripts that move files to certain directories on the windows server that begin with "data2/". That has changed, they are removing "data2/" from the path, not to mention different servers but thats not my question.

I have about 30 production unix scripts to modify in a certain unix directory that contain "data2/" instances. I cant rename the scripts, I basically have to remove the "data2"/ from each one. Is there a command I can run from the prompt, or do I have to do each one with VI. Either way you guys advise to be easier, how do you do it?
  #5 (permalink)  
Old 03-06-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Quote:
Originally Posted by NycUnxer View Post
After running a command like

grep -ir files2/ *

This will find all the files that contain "files2/" in it.

For example if it finds

files2/dir/today
files2/dir/yesterday
files2/dir/2daysago

Now it may find 100 instances, so is there a quick find and replace command I can use? I basically want to remove "files2" from all instances. tia.
Code:
grep -ir files2/ * > list.txt
while read file
do
  sed -i -e "s/files2\///g" $file
done < list.txt
You need to have GNU sed for the -i flag. If you dont have that, replace sed with perl.
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:57 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