The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pattern matching problem in PERL script ganapati Shell Programming and Scripting 11 10-15-2008 04:17 PM
Pattern Matching problem in UNIX maxmave Shell Programming and Scripting 2 06-03-2008 02:19 AM
pattern matching problem namishtiwari Shell Programming and Scripting 2 05-23-2008 08:33 AM
problem with CASE pattern matching gummysweets Shell Programming and Scripting 2 03-18-2008 11:30 AM
pattern matching problem rein Shell Programming and Scripting 8 10-27-2007 12:44 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-21-2009
lassimanji lassimanji is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 58
problem using sed for pattern matching

if abc.sh is
192.168.1.41

then the output that i get is v5c01

my code is
sed 's/192.168.1.4/v5c0/g
s/192.168.1.41/acc1/g' abc.sh 2>&1 | tee abc.sh

i want to find 192.168.1.4 and replace it with v5c0
and find 192.168.1.41 and replace it with acc1

and i want to do it using sed
  #2 (permalink)  
Old 04-21-2009
TonyFullerMalv's Avatar
TonyFullerMalv TonyFullerMalv is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Location: Malvern, Worcs. U.K.
Posts: 748
Nearly there:

Code:
rm abc.sh.$$ 2>/dev/null
sed -e 's/192.168.1.4/v5c0/g' -e 's/192.168.1.41/acc1/g' abc.sh > abc.sh.$$ && \
cp abc.sh.$$ abc.sh && \
rm abc.sh.$$

  #3 (permalink)  
Old 04-21-2009
lassimanji lassimanji is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 58
sed 's/'^192.168.1.4$'/v5c0/g
s/192.168.1.41/acc1/g' abc.sh 2>&1 | tee abc.sh

it works with this
  #4 (permalink)  
Old 04-22-2009
lassimanji lassimanji is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 58
can you explain what your code does..mine seemed to be working yesterday but I see the same problem with it again today
  #5 (permalink)  
Old 04-22-2009
TonyFullerMalv's Avatar
TonyFullerMalv TonyFullerMalv is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Location: Malvern, Worcs. U.K.
Posts: 748
The script removes the temporary file we are going to use, ensuring it is not a symbolic link pointing elsewhere, the the sed line does:
for every line in abc.sh it looks for
192.168.1.4 and replaces it with v5c0 however many times 192.168.1.4 occurs in each line, without the "g" only the first occurrence in each line would get replaced, and: sed looks for 192.168.1.41 and replaces and replaces it with acc1 however many times it repeated in each line,
the output from sed is put into a temporary file because normal text processing commands cannot write back to the file they are reading from without corrupting them, the temporary filename is abc.sh. with the PID of the script added on the end to make it unique, the ">" means that if abc.sh.$$ already existed it will get overwritten, if the sed does not fail (the && test) then the next command is run which is to copy the temporary file back to the original, copying the temporary file rather than moving it preserves the permissions of abc.sh, if the copy is successful then the temporary file is deleted.
  #6 (permalink)  
Old 04-22-2009
lassimanji lassimanji is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 58
thanks for the detailed explanation
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 05:51 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