Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

How to use Regular expression

Hi

How would i use regular expressions to change text in vi. For example I have file.txt file and I want to change word block to unblock.  Please give me whole syntax since I am begginer with Regular expressions. I found some stuff on google but it is all too advanced. I dont understand people that write things for experts cause for the hell sake expert doesnt need instructions. But for begginer there is nothing on the whole internet how to learn regular expressions.

How would i replace above mentioned word in text file

:%s/\<block\>/unblock/g I tried this as advised by one guy but bash is giving me error

0
cer113
Asked:
cer113
  • 2
1 Solution
 
farzanjCommented:
:%s/block/unblock/g
0
 
farzanjCommented:
So you know that you will open file using vi and then in the command mode would type this command and hit enter.  That should take care of it.
:%s/block/unblock/g

Open in new window



If you don't understand this, simply issue the following command on the command line (shell


 
sed -i 's/block/unblock/g' file.txt

Open in new window

0
 
cer113Author Commented:
yes this was working all good word is replaced i opened vi file.txt and in command line typed the command and word got replaced that is good
0
 
lexlythiusCommented:
WARNING: farzani's tip works but will replace block with unblock even if it is not a whole word. For example if your file read:
What kind of block does a blockbuster bust?

Open in new window

you would get:
What kind of unblock does a unblockbuster bust?

Open in new window


To avoid this problem, try this instead
:%s/\bblock\b/unblock/g

Open in new window


\b asserts "word boundaries" (non-word characters or start of line/end of line)

A good tour of Regular Expressions is PHP's http://www.php.net/manual/en/regexp.introduction.php  actually based on Perl's Regular Expression, which is a superset of the basic regular expressions in every Unix.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now