Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Linux and Unix
Tag Cloud
access acer asus bios bsod computer crash desktop dns driver drivers error ethernet excel freeze gaming graphics hard drive hardware hdmi internet laptop malware memory monitor motherboard network printer problem ram registry repair router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > Linux and Unix >
Unix Script Help!!!

Reply  
Thread Tools
lboogie's Avatar
Junior Member with 1 posts.
 
Join Date: Mar 2007
Experience: Beginner
10-Apr-2007, 12:38 PM #1
Question Unix Script Help!!!
I am trying to write script that search for a string in a given file, if the string is found the script has to make a substitution and backup file, if string is not found no change is to be made. I am banging my head against the wall. Please Help!!!
lotuseclat79's Avatar
Distinguished Member with 21,345 posts.
 
Join Date: Sep 2003
Location: -71.45091, 42.27841
10-Apr-2007, 12:53 PM #2
Quote:
Originally Posted by lboogie
I am trying to write script that search for a string in a given file, if the string is found the script has to make a substitution and backup file, if string is not found no change is to be made. I am banging my head against the wall. Please Help!!!
Hi lboogie,

Welcome to TSG!

Look up the man page for egrep: man egrep

Then use the egrep command to find the string in your file, and testing the return value of the egrep command, you can determine if the string was found or not and do the appropriate thing using the if then else construct in your script.

Now, if you have to start in a particular directory structure and decend and look in all of the files in subordinate directories, then look at the man page for the "find" command: man find

-- Tom
__________________
The independence created by philosophical insight is - in my opinion - the mark of distinction
between a mere artisan or specialist and a real seeker after truth. - Einstein 1944
Imagination is more important than knowledge. - Einstein
Headrush's Avatar
Senior Member with 1,335 posts.
 
Join Date: Feb 2005
Experience: Advanced
15-Apr-2007, 10:50 AM #3
Quote:
Originally Posted by lboogie
I am trying to write script that search for a string in a given file, if the string is found the script has to make a substitution and backup file, if string is not found no change is to be made. I am banging my head against the wall. Please Help!!!
Check out sed instead.

Its as simple as something like this: sed 's/string/newstring/g' originalfile.txt > newfile.txt

This will create a new file even if no changes are made but its trivia to deleted if empty.
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 08:01 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.