How to use Regular expression

Posted on 2011-05-12
Last Modified: 2012-05-11

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

Question by:cer113
    LVL 31

    Expert Comment

    LVL 31

    Accepted Solution

    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.

    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


    Author Comment

    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
    LVL 7

    Expert Comment

    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

    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  actually based on Perl's Regular Expression, which is a superset of the basic regular expressions in every Unix.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
    The purpose of this article is to demonstrate how we can use conditional statements using Python.
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now