Replace everything but pattern in a line using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace everything but pattern in a line using sed
# 1  
Old 04-05-2012
Replace everything but pattern in a line using sed

I have a file with multiple lines like this:

Code:
<junk><PATTERN><junk><PATTERN><junk>
<junk><PATTERN><junk><PATTERN><junk><PATTERN><junk>

Note that
1. There might be variable number occurrences of PATTERN in a line.
2. <> are just placeholders, they do not form part of the pattern.

I need to replace all the <junk> with a nice separator, like a comma.

So I am left with, in the above case

Code:
,PATTERN,PATTERN,
,PATTERN,PATTERN,PATTERN,

In my case, PATTERN has the pattern "AB:" followed by 9 digits.

I am able to replace the pattern with comma, using the following command -

Code:
sed 's/\(AB\:[0-9]\{9\}\)/,/g' <input_file>

So I thought replacing everything BUT the pattern should be just one step further - but I am not able to find a solution. Isn't there a way to do this using sed, using the ! operator? I am not able to put my finger on the solution. Any help would be greatly appreciated!

Thanks

Moderator's Comments:
Mod Comment Welcome to the UNIX and Linux Forums. Please use code tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-05-2012 at 09:28 PM.. Reason: Code tags
# 2  
Old 04-05-2012
Sometimes you can use some tricks to add extra data to break patterns out like this.. using for example, control characters (needs to be a character not found in the data)... consider:

Code:
sed -e 's/PATTERN/^BPATTERN^B/g' <test2.txt | tr '\012\002' '\002\012' | grep -v '^PATTERN$' | tr -d '\012' | tr '\002' '\012'

Where your data is in test2.txt and in the first sed the ^B are literally Ctrl-B characters... this breaks the patterns out onto lines to themselves and replaces newlines with Ctrl-B and stitches things back together afterwords.
# 3  
Old 04-05-2012
You are not per chance only interested in the patterns themselves or do they need to remain on their original lines? Otherwise if your system has grep -o you can do this:
Code:
grep -Eo 'AB:[0-9]{9}' infile

and if you system does not have grep -o, you could do this:

Code:
sed 's/AB:[0-9]\{9\}/\n&\n/g' infile | grep -E 'AB:[0-9]{9}'

or for older sed:
Code:
sed 's/AB:[0-9]\{9\}/\
&\
/g' infile | grep -E 'AB:[0-9]{9}'

# 4  
Old 04-05-2012
Code:
awk 'gsub("<junk>",",")' Your_File

# 5  
Old 04-06-2012
Thanks for all the replies!

Scrutinizer, I need all the patterns that occurred in a line, and they need to remain on the original lines, with the junk filtered out. I did try out the grep -o option, but then I couldn't tell which line a given pattern corresponds to.

codecaine, the junk follows no particular pattern, so I can't delete the junk using sed. Smilie

cjcox, I see what you mean - and how it would work. I was wondering if there would be a more straightforward solution. If I can replace all occurrences of a pattern with a string of my choice, surely I'd expect to be able to replace everything BUT the pattern with a small tweak of the command? Or is it not that straightforward?

Thanks again for all the help, really appreciate it! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace two character pattern with another pattern

Not able to paste my content. Please see the attachment :-( (2 Replies)
Discussion started by: vivek d r
2 Replies

2. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

3. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

4. Shell Programming and Scripting

Sed replace using same pattern repeating multiple times in a line

Sed replace using same pattern repeating multiple times in a line I have text like below in a file: I am trying to replace the above line to following How can I acheive this? I am able to do it if the occurrence is for 1 time: But If I try like below I am getting like this: I have to... (4 Replies)
Discussion started by: sol_nov
4 Replies

5. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

6. Shell Programming and Scripting

pattern match and replace another pattern in same line

I have a pattern username:x:32005:32006::/usr/local/user:/bin/bash I need to match the line containing username and replace /bin/bash with /usr/local/my/bin/noshell So it becomes username:x:32005:32006::/usr/local/user:/usr/local/my/bin/noshell (7 Replies)
Discussion started by: anilcliff
7 Replies

7. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

8. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: TestTomas
5 Replies

9. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question