- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- delete exact word
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2005 07:50 AM
05-26-2005 07:50 AM
delete exact word
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2005 07:54 AM
05-26-2005 07:54 AM
Re: delete exact word
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2005 08:13 AM
05-26-2005 08:13 AM
Re: delete exact word
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2005 10:42 AM
05-26-2005 10:42 AM
Re: delete exact word
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2005 02:06 PM
05-26-2005 02:06 PM
Re: delete exact 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2005 06:49 PM
05-26-2005 06:49 PM
Re: delete exact word
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.