sed: Find start of pattern and extract text to end of line, including the pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed: Find start of pattern and extract text to end of line, including the pattern
# 1  
Old 05-27-2009
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  
Old 05-27-2009
Code:
sed '/-[EW]-/s/.*-[EW]-\(.*\)/\1/' input.txt

# 3  
Old 05-27-2009
I have done a few modifications to your solution, try this:
Code:
grep -e '-[EW]-' input.txt | sed 's/^.*\(-[EW]-\)/\1/'

# 4  
Old 05-27-2009
Quote:
Originally Posted by edgarvm
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  
Old 05-27-2009
It did the trick, thank you very, very much Smilie

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  
Old 05-27-2009
sorry - try this:
Code:
sed -n '/-[EW]-/s/.*-[EW]-\(.*\)/\1/p' input.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed Range Pattern and 2 lines before Start Pattern

Hi all, I have been searching all over Google but I am unable to find a solution for a particular result that I am trying to achieve. Consider the following input: 1 2 3 4 5 B4Srt1--Variable-0000 B4Srt2--Variable-1111 Srt 6 7 8 9 10 End (3 Replies)
Discussion started by: y2jacky
3 Replies

2. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. Shell Programming and Scripting

sed: how to move matched pattern to end of previous line

Hello, I'm new to this forum. I've been doing a lot of sed work lately and have found many useful tips on this forum. I've hit a roadblock in a project, though, and could really use some help. I have a text file with many lines like the following, i.e., some lines begin with a single word... (3 Replies)
Discussion started by: paroikoi
3 Replies

4. Shell Programming and Scripting

Sed : identify a pattern and append a word at the end of a line

Hello to all, On aix, I want to identify a term on a line in a file and then add a word at the end of the line identified. I do not want the word to be added when the line contains the symbol "#". I use the following command, but it deletes the term identified then adds the word. #sed... (4 Replies)
Discussion started by: dantares
4 Replies

5. Shell Programming and Scripting

Extract pattern from text line

The text line has the following formats: what.ever.bla.bla.C01G06.BLA.BLA2 what.ever.bla.bla.C11G33.BLA.BLA2 what.ever.bla.bla.01x03.BLA.BLA2 what.ever.bla.bla.03x05.BLA.BLA2 what.ever.bla.bla.Part01.BLA.BLA2 and other similar ones, I need a way to select the "what.ever.bla.bla" part out... (4 Replies)
Discussion started by: TehOne
4 Replies

6. Shell Programming and Scripting

Extract pattern from text line

Hi, the text line looks like this: "test1" " " "test2" "test3" "test4" "10" "test 10 12" "00:05:58" "filename.bin" "3.3MB" "/dir/name" "18459" what's the best way to select any of it? So I can for example get only the time or size and so on. I was trying awk -F""" '{print $N}' but... (3 Replies)
Discussion started by: TehOne
3 Replies

7. Shell Programming and Scripting

Extract pattern from text line

Gents, from these sample lines: ZUCR.MI ZUCCHI SPA RISP NC 2,5000 6 ott 0,0000 ZV.MI ZIGNAGO VETRO 3,6475 16:36 Up 0,0075 is it possible to get this: ZUCR.MI 2,5000 ZV.MI 3,6475 i.e. the first field, a separator and the first decimal number? (in Europe we... (9 Replies)
Discussion started by: vampirodolce
9 Replies

8. UNIX for Advanced & Expert Users

Urgent Help required : awk/sed help to find pattern and delete till end of line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (1 Reply)
Discussion started by: rajan_san
1 Replies

9. Shell Programming and Scripting

Urgent! Sed/Awk Filter Find Pattern Delete Till End Of Line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (2 Replies)
Discussion started by: rajan_san
2 Replies

10. Shell Programming and Scripting

extract a particular start and end pattern from a line

hi In the foll example the whole text in a single line.... i want to extract text from IPTel to RTCPBase.h. want to use this acrooss the whole file Updated: IPTel\platform\core\include\RTCPBase.h \main\MWS2051_Sablime_Int\1... (7 Replies)
Discussion started by: manish205
7 Replies
Login or Register to Ask a Question