The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
find pattern, delete line with pattern and line above and line below nickg Shell Programming and Scripting 4 01-29-2009 01:38 PM
Extract pattern from text line TehOne Shell Programming and Scripting 4 12-25-2008 01:25 PM
Extract pattern from text line TehOne Shell Programming and Scripting 3 10-15-2008 01:46 PM
Extract pattern from text line vampirodolce Shell Programming and Scripting 9 10-14-2008 12:17 PM
extract a particular start and end pattern from a line manish205 Shell Programming and Scripting 7 02-07-2008 06:18 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 05-27-2009
TestTomas TestTomas is offline
Registered User
  
 

Join Date: May 2009
Posts: 2
sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials.

The task at hand is:

Input file input.txt (example)

abc123defhij-E-1234jslo
456ujs-W-abXjklp

From this file the task is to grep the -E- and -W- strings that are unique and write a new file starting with the matched pattern (-E-, -W-)

The end result should look like this:

-E-1234jslo
-W-abXjklp

The closest I have come to do this is using this code:

Code:
grep -e '-[EW]-' input.txt | sed 's/.*'-[EW]-'//'
The output looks like this:

1234jslo
abXjklp

The problem is that this doesn't give me the -E- and -W- that is part of the regular expression. I guess I need a way to put in the matched part into the replace part of sed.

Thanks in advance for any help.

Last edited by TestTomas; 05-27-2009 at 10:33 AM.. Reason: Corrected spelling error
  #2 (permalink)  
Old 05-27-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Code:
sed '/-[EW]-/s/.*-[EW]-\(.*\)/\1/' input.txt
  #3 (permalink)  
Old 05-27-2009
edgarvm edgarvm is offline
Registered User
  
 

Join Date: May 2009
Posts: 26
I have done a few modifications to your solution, try this:
Code:
grep -e '-[EW]-' input.txt | sed 's/^.*\(-[EW]-\)/\1/'
  #4 (permalink)  
Old 05-27-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Quote:
Originally Posted by edgarvm View Post
I have done a few modifications to your solution, try this:
Code:
grep -e '-[EW]-' input.txt | sed 's/^.*\(-[EW]-\)/\1/'
I don't think it produces the results the OP is after.
Also there's no need to use 'grep' when sed can do the same.
  #5 (permalink)  
Old 05-27-2009
TestTomas TestTomas is offline
Registered User
  
 

Join Date: May 2009
Posts: 2
It did the trick, thank you very, very much

Edit:
Actually the pure sed-version 'fixed' the lines with the matching pattern but also printed out all non matching lines while the version with grep worked perfectly

Last edited by TestTomas; 05-27-2009 at 11:55 AM.. Reason: Extended the information.
  #6 (permalink)  
Old 05-27-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
sorry - try this:
Code:
sed -n '/-[EW]-/s/.*-[EW]-\(.*\)/\1/p' input.txt
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 09:18 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