How to negate pattern within sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to negate pattern within sed?
# 1  
Old 08-14-2013
How to negate pattern within sed?

Example:
I have data like,
Code:
H|1|2|#||4|4|5|6
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
T|g|g|G|G|g|g|
T|g|g|G|G|g|g|

I have to write command, it should delete all the lines except line starting with "D".

I have tried sed '/^\(D\)|/!d' filename. But its not working as expected.

Is there any other way of doing this?

Last edited by Franklin52; 08-14-2013 at 10:53 AM.. Reason: Please use code tags
# 2  
Old 08-14-2013
Code:
$ cat a.txt
H|1|2|#||4|4|5|6
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
T|g|g|G|G|g|g|
T|g|g|G|G|g|g|

$ sed '/^D/!d' a.txt
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|

# 3  
Old 08-14-2013
Code:
$
$ cat a.txt
H|1|2|#||4|4|5|6
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
T|g|g|G|G|g|g|
T|g|g|G|G|g|g|
$
$ sed '/^[^D]/d' a.txt
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
D|f|g|h|j|j|k|k|
$
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 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 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

4. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

5. 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

6. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

7. Shell Programming and Scripting

sed pattern to delete lines containing a pattern, except the first occurance

Hello sed gurus. I am using ksh on Sun and have a file created by concatenating several other files. All files contain header rows. I just need to keep the first occurrence and remove all other header rows. header for file 1111 2222 3333 header for file 1111 2222 3333 header for file... (8 Replies)
Discussion started by: gary_w
8 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

negate search within sed

I working fine with following statement - sed '/search_pattern/ s/pattern1/pattern2/' file_name requirement changes , now i want negate the search something like - sed '! /search_pattern/ s/pattern1/pattern2/' file_name (this doesn't work) anybody can plz tell the correct syntax... (2 Replies)
Discussion started by: ajitkumar2
2 Replies

10. Shell Programming and Scripting

how do I negate a sed match

I have a text file that has links in it. I can write a match for sed to replace the link with anything. For example: http://www.google.com becomes XxX But what I'm after is not to replace the link with something but to remove everything else and just leave the link. I want a... (5 Replies)
Discussion started by: muxman
5 Replies
Login or Register to Ask a Question