LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-18-2006, 03:29 AM   #1
ryzor
LQ Newbie
 
Registered: Mar 2006
Posts: 16

Rep: Reputation: 0
sh script not executing as expected


Hi all

i want to execute this scrip on sles 10 - i want to change the occurency of OLD_NAME ...but all what this scrip do is to list the files containting "flyingsk1" only - it does not change them as desired.
______________________________________________
#!/bin/sh
OLD_NAME=flyingsk1
WAS_DIR=/opt/ibm/websphere
NEW=flyingskupdated2

grep -rl ${OLD_NAME} ${WAS_DIR}
ed - $FILE <<!EOF
g/${OLD_NAME}/s//${NEW}/
w
q
!EOF
_______________________________________________

is there something i'm missing here?

regards,
ryzor.
 
Old 12-18-2006, 04:59 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
I'm not sure I understand exactly what you want to do... Please tell me if this is correct:
You want to look in all files inside the directory /opt/ibm/websphere (including sub-directories), and for every file containing the string "flyingsk1", you want to replace every occurrence of this string with the string "flyingskupdated2"?
 
Old 12-18-2006, 05:19 AM   #3
ryzor
LQ Newbie
 
Registered: Mar 2006
Posts: 16

Original Poster
Rep: Reputation: 0
yes thats exactly what i want to achieve...
 
Old 12-18-2006, 05:50 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
There are probably loads of ways of doing this. Here's how I'd approach it:
  1. BACKUP your files!
  2. Use find to get a list of the files in the sub-directories of /opt/ibm/websphere
  3. use xargs and fgrep (with -l option) on the list of files to identify which files contain the string we want to replace
  4. use xargs and sed to modify those files.
This can all be done in a single pipeline

Code:
find /opt/ibm/websphere -type f -print0 | \
  xargs -0 fgrep -l -Z flyingsk1 | \
  xargs -0 sed -i 's/flyingsk1/flyingskupdated2/g'
OK, here's how it works:
  • find lists all the files we want to check. The -print0 option means to delimit the output with the NUL (\0) character. This prevents problems if some of the file names have spaces, newlines, tabs or other weird character in them.
  • xargs with the -0 option read the list of \0 delimited file names and passes them as arguments to "fgrep -l -Z flyingsk1"
  • "fgrep -l -Z flyingsk1" takes the files specified and checks to see if any of them contain the string "flyingsk1". If a file contains the string, the -l option tells fgrep to prints the filename. The -Z option makes the output delimited with the \0 character.
  • The second xargs feeds the list of files (now just those which contain our string) to the sed command.
  • The sed command edits the files in-place (that's what the -i option is for) making the substitution as required.
 
Old 12-18-2006, 09:03 AM   #5
ryzor
LQ Newbie
 
Registered: Mar 2006
Posts: 16

Original Poster
Rep: Reputation: 0
thanx Mathewg42.

it worked
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unary Operator expected. Bash script Blackout_08 Programming 2 06-22-2006 02:21 PM
executing script with ./ does not work Lotharster Linux - Newbie 7 05-14-2006 05:27 PM
Bash script - executing a script through subdirectories bubkus_jones Programming 5 04-24-2006 05:05 PM
executing a script!! vishamr2000 Linux - Security 5 07-29-2005 02:34 AM
Script Problem - Does not give expected results Jose Muņiz Linux - General 3 07-12-2003 10:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:11 PM.

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
Open Source Consulting | Domain Registration