1834144 Members
2276 Online
110064 Solutions
New Discussion

delete exact word

 
Krish_4
Contributor

delete exact word

Hi,

1) I have a line something like this

word abxrage word xra yyxrakba

word xra word word

Want to delete only word xra. Whatever RE I am trying it deletes all occurences of xra.

2) Also I want to delete second & onwards occurence of word xra in a file.

Any help is appreciated.

Thanks.
5 REPLIES 5
Kent Ostby
Honored Contributor

Re: delete exact word

Krish ..

If you can use vi, then you could use three steps:

:1,$:s/ xra //
:1,$:s/^xra //
:1,$:s/ xra$//

Best regards,

Kent M. Ostby
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Rick Garland
Honored Contributor

Re: delete exact word

Can use grep -x command. The -x switch is EXACT match. From here you can pipe into sed or tr, etc
Mel Burslan
Honored Contributor

Re: delete exact word

assuming your text is in file1

cat file1 | sed -e "1,\$s/ xra / /g" >file2
cat file2 > file1
cat file1 | sed -e "1,\$s/ xra$//>file2
cat file2 > file1
cat file1 | sed -e "1,\$s/^xra //>file2
cat cat file2 > file1


this should take care of the job
________________________________
UNIX because I majored in cryptology...
Hein van den Heuvel
Honored Contributor

Re: delete exact word

You have to carefully define what makes a word.
Is it delimited by spaces only like in your example?
Could it be delimited by a line begin or end?
Could it be delimited by a punctuation mark?Any whitespace?
What do you want done with the whitespace?

Your example, with simple spaces is solved in perl using:

# perl -itmp -pe "s/ xra / /g" tmp.txt

-p is for 'loop through input and print each line'

The -i is for 'in place'.
Alternative is:

# perl -pe "s/ xra / /g" tmp.txt > new.txt

Now if you want every single word xra, but not the word parts xra to be replaced the most powerful regexpr component to us is \b which is zero-length word-non-word boundary. Solution with that:
# perl -pe "s/\bxra\b//g" tmp.txt

This leaves whitespace as it was, so space-xra-space becomes space-space.

demo (on xp) below.
Hth,
Hein.

#
# type tmp.txt
word abxrage word xra yyxrakba
word xra word word
Want to delete only word xra. Whatever RE I am trying it deletes all occurences
of xra.
xra must go. No more xra

# perl -itmp -pe "s/ xra / /g" tmp.txt

# type tmp.txt
word abxrage word yyxrakba
word word word
Want to delete only word xra. Whatever RE I am trying it deletes all occurences
of xra.
xra must go. No more xra

# perl -itmp -pe "s/\bxra\b//g" tmp.txt

# type tmp.txt
word abxrage word yyxrakba
word word word
Want to delete only word . Whatever RE I am trying it deletes all occurences of
.
must go. No more

Muthukumar_5
Honored Contributor

Re: delete exact word

You can sed as,

sed 's/ xra / /g;s/^xra[ ]*//g;s/ xra$//g' >

mv

Else directly as,

sed -i 's/ xra / /g;s/^xra[ ]*//g;s/ xra$//g'

Example:
=======
$ cat testfile
word abxrage word xra yyxrakba
word xra word word
word abxrage word xra yyxrakba xra
xra word
$ sed 's/ xra / /g;s/^xra[ ]*//g;s/ xra$//g' testfile
word abxrage word yyxrakba
word word word
word abxrage word yyxrakba
word

hth.
Easy to suggest when don't know about the problem!