how do I negate a sed match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how do I negate a sed match
# 1  
Old 09-29-2006
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:
Code:
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 negative match. So that everything that matches is left alone but all non-matching text is removed.

Any ideas?
# 2  
Old 09-29-2006
Code:
sed "/http:\/\/www.google.com/!d" file

! - negate the match
# 3  
Old 09-29-2006
Quote:
Originally Posted by anbu23
Code:
sed "/http:\/\/www.google.com/!d" file

! - negate the match
That deletes the match. What I'm after is to negate it. Instead of replacing the match, replace EVERYTHING ELSE and leave the match alone.
# 4  
Old 09-29-2006
can you show some example?
# 5  
Old 09-29-2006
Quote:
Originally Posted by muxman
That deletes the match. What I'm after is to negate it. Instead of replacing the match, replace EVERYTHING ELSE and leave the match alone.
Alternative in Python:
Code:
#!/usr/bin/python
all = open("file1.txt").read()
allwords = all.split()
for i in range(0,len(allwords)):
	if not "http://www.google.com" in allwords[i]:
 		allwords[i] = "xXx"
print ' '.join(allwords)

Output:
Code:
'xXx xXx http://www.google.com xXx xXx xXx'

# 6  
Old 10-04-2006
use the -n option of sed, for example:

sample file link.txt contains:
text
http://www.google.com
text
https://www.unix.com
last line

filter file link.flt contains:
1,$ {
/http:\/\// {
p
}
}

$ sed -n -f link.flt link.txt
http://www.google.com
https://www.unix.com
$
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

2. Shell Programming and Scripting

How to get the negate of decimal to binary?

Hi All, New to this forum (and yes , a newbie in programming..:p) I have a decimal to binary converter script done this way : i=$1 bit0=$(( (i & 0x01) > 0 )) bit1=$(( (i & 0x02) > 0 )) bit2=$(( (i & 0x04) > 0 )) bit3=$(( (i & 0x08) > 0 )) bit4=$((... (6 Replies)
Discussion started by: digiteltlc
6 Replies

3. Shell Programming and Scripting

How to negate pattern within sed?

Example: I have data like, 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'... (2 Replies)
Discussion started by: duplicate
2 Replies

4. Web Development

Negate user space URL in Apache

Hello, I have a situation where I am trying to use Apache's RedirectMatch directive to redirect all users to a HTTPS URL except a single (Linux) user accessing there own webspace. I have found a piece of regular expression code that negates the username: ^((?!andy).)*$but when I try using it... (0 Replies)
Discussion started by: LostInTheWoods
0 Replies

5. Shell Programming and Scripting

Negate alerting for particular metadevice.

Hi All... I have a script that checks for any problems(particularly looks for 'Needs Maintenance') with metadevices and alerts accordingly. This was not configured to alert for a particular metadevice. Now i want to negate alerting for a particular metadevice(say d40). Is this possible? I am... (6 Replies)
Discussion started by: reddybs
6 Replies

6. Shell Programming and Scripting

negate search help

Hi, I've tried a lot of negate codes in this forum, but they do not perform what I intended. Please help. inputfile: Paragraph1 contents: die1, die2, die3, pr_name1, pr_name2 pr_name3, pr_name4 Paragraph2 more contents: die1, die2, die3, pr_name1, pr_name2 pr_name3, pr_name4 ... (5 Replies)
Discussion started by: shamushamu
5 Replies

7. Shell Programming and Scripting

Negate gawk search

Hi, I am using the under-noted script to search the "MYPATTERN" in MYFILE and print that block of lines containing the pattern starting with HEADER upto FOOTER. Please help me what to put in script to negate the search i.e. not to print those blocks meeting the search criteria. gawk -v... (1 Reply)
Discussion started by: vanand420
1 Replies

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

9. Shell Programming and Scripting

How to negate grep result?

Here is my script so far: set dirs = ` find . -name "message.jar" 2> /dev/null | cut -d "/" -f 2 ` | uniq foreach dir ( $dirs ) if (grep $dir/* someText==null) --> how do I write this in script? print $dir end end (4 Replies)
Discussion started by: mmdawg
4 Replies

10. Shell Programming and Scripting

negate * with in pattren matching...

Hi Every one I have a file in the following manner... AAAAAA*PERFORM WRITEQ BBDFDD*PERFOMF WRITEQ FFFF *PERFOMF WRITEQ i want to find the lines which donot have * in 7th position.. I have tried this but some problem i think... grep '......*WRITEQ' INpFIle... any 6 chars not... (7 Replies)
Discussion started by: pbsrinivas
7 Replies
Login or Register to Ask a Question