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
counting the lines matching a pattern, in between two pattern, and generate a tab d.chauliac Shell Programming and Scripting 4 03-19-2009 01:30 PM
pattern matching adityamahi Shell Programming and Scripting 7 12-30-2008 10:02 AM
comment/delete a particular pattern starting from second line of the matching pattern imas Shell Programming and Scripting 4 10-13-2008 03:37 AM
Pattern matching blue_bird UNIX and Linux Applications 3 10-08-2008 02:23 AM
pattern matching in an if-then lumix Shell Programming and Scripting 4 12-14-2007 04:25 PM

Reply
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 4 Weeks Ago
steadyonabix steadyonabix is online now
Registered User
  
 

Join Date: Oct 2009
Location: UK
Posts: 185
sed pattern matching

Unfortunately this chap has been banned for some reason and I was looking forward to the resolution of his question: -

append the position 28:33

He was asking if you can use sed to match a pattern you want to replace within a restricted range of characters in a string. Can anyone tell me how to do this?

He wanted to change: -


Code:
21111111110001343 000001004OLF-AA029100020091112

to: -


Code:
21111111110001343 000001004OLF-1E029100020091112

But I don't want a generic match that matches the whole line, just that range highlighted in blue. Is it possible to do this using sed?
  #2 (permalink)  
Old 4 Weeks Ago
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,342
Something like this?


Code:
sed 's/.*\(...-..\).*/\1/'

  #3 (permalink)  
Old 3 Weeks Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 744
But I like regex

Code:
ssed -r 's/(.{27}).{6}/\1FOO-BR/' infile


Last edited by Scrutinizer; 3 Weeks Ago at 07:17 PM..
  #4 (permalink)  
Old 3 Weeks Ago
steadyonabix steadyonabix is online now
Registered User
  
 

Join Date: Oct 2009
Location: UK
Posts: 185
Thanks for the replies but I don't think that they quite do what I am asking for. I want to combine the replacement of the characters within the range while matching the pattern.

So the replacement only occurs if the pattern is present within that specific range of characters.
  #5 (permalink)  
Old 3 Weeks Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 744
Hi steady, you mean something like this?

Code:
sed -r 's/(.{27})OLF-AA/\1OLF-1E/;s/(.{27})ODL-SP/\1ODL-2S/;s/(.{27})ODL-CH/\1ODL-3C/' infile


Last edited by Scrutinizer; 3 Weeks Ago at 07:37 PM..
  #6 (permalink)  
Old 3 Weeks Ago
steadyonabix steadyonabix is online now
Registered User
  
 

Join Date: Oct 2009
Location: UK
Posts: 185
That's the one, thanks!

Knew it could be done......
  #7 (permalink)  
Old 3 Weeks Ago
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,556
Quote:
Originally Posted by steadyonabix View Post
Unfortunately this chap has been banned for some reason and I was looking forward to the resolution of his question: -

append the position 28:33

He was asking if you can use sed to match a pattern you want to replace within a restricted range of characters in a string. Can anyone tell me how to do this?

He wanted to change: -


Code:
21111111110001343 000001004OLF-AA029100020091112

to: -


Code:
21111111110001343 000001004OLF-1E029100020091112

But I don't want a generic match that matches the whole line, just that range highlighted in blue. Is it possible to do this using sed?
putting tools to use aside, let's think logically. if you want to replace position 28 to 33, then print from position 1 to 27, then print the new replacement, then print from 34 onwards...
pseudocode:

Code:
print substr(line,1,27) "OLF-1E" substr(line,34)

no need to think of any regex.
Reply

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 06:38 PM.


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