LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-18-2012, 04:47 AM   #1
ingridsvensson
LQ Newbie
 
Registered: Sep 2012
Posts: 3

Rep: Reputation: Disabled
Talking Doing a "find and replace all" in all files in a directory


Hi,

Lets say that I have a directory with say 10 files.

Question
--------
I would like to find the word "Austria" in all these files and replace it with "Germany".

Note that I do not have perl in my cygwin distribution and so perl based commands do not work.

What is the command that will help me achieve the desired goal.

Thank you
Ingrid
 
Old 09-18-2012, 05:21 AM   #2
ynodelman
LQ Newbie
 
Registered: Sep 2008
Posts: 4

Rep: Reputation: 1
find . -name "*matching pattern*" -exec sed -i "s/Austria/Germany/g" '{}' \;
 
Old 09-18-2012, 05:32 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
First----cygwin is not really a Linux distro---it simply allows you to do some "Linux things" within Windows.

I'm assuming that you are using the BASH shell within Cygwin, and that the standard tools--eg SED-- are available.

If you want to operate on all files in the current directory, then this should work:
Code:
sed -i 's/Austria/Germany/g' *
this edits the files "in place"---ie the old version is not saved. To save to new names, it's a bit more involved.
 
Old 09-18-2012, 10:19 AM   #4
ingridsvensson
LQ Newbie
 
Registered: Sep 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks for the replies. But this does not solve the underlying problem that I am trying to address.

I have a file. Lets Call it File1.

Step 1
======>
File1a = File generated by executing the following command : sed -i 's/obj/ /g' File1.txt

File1b = File generated by doing a "replace all" for the keyword 'obj' on File 1.

Note that in essence this step involves removing the string "obj" everywhere in File1 i.e. replacing "obj" with nothing.

Step 2
======>
File 2a = Resultant file obtained by doing a "grep -a Title File1a.txt"

File 2b = Resultant file obtained by doing a "grep -a Title File1b.txt"

The strange observation is that File 2a ~= File2b (~= does not equal)

I can make this description meaningful by uploading the actual files on to this site. However there is no space for any attachments and so I was wondering if I could send it to one of the repliers by email so that they can take a look at it.

Clarification:
I want to understand the reason as to why File2a ~= File2b. I need the result as described in File2b but the drawback of that is that I have to manually open the file and do the replace all. Find a command that enabled to have the File2b format is the best solution.

I welcome any new comments/clarifications.

Ingrid

P.S:The find and replace was done using wordpad
Also I see that it is possible to upload files to this site but the file sizes allowed are too small for the files that I want to upload. I have a zip file (332 Kb) containing the following 5 files:
Contents:
File1.txt
File1a.txt
File1b.txt
File2a.txt
File2b.txt
that I can send to any one interested by email.

Last edited by ingridsvensson; 09-18-2012 at 10:40 AM.
 
Old 09-18-2012, 09:02 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,362

Rep: Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377
This
Code:
sed -i 's/obj/ /g' File1.txt
should replace the string 'obj' with a single space.

1. is that what you wanted ie space != "nothing" eg
replace with nothing ie delete string; no replacement
Code:
sed -i 's/obj//g' File1.txt

2. exactly how did you do the 'replace all' cmd?
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add the "More Apps" and "Find Files" buttons to the 11.10 Launcher Larry James Ubuntu 0 10-19-2011 12:42 AM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 03:07 AM
1. shell script "find and replace" on text 2. java GUI application randomx Programming 4 03-05-2004 01:01 PM


All times are GMT -5. The time now is 10:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration