Results 1 to 5 of 5

Thread: how to replace word with dot into underscore?

  1. #1
    Member
    Join Date
    Jun 2012
    Location
    CHENNAI
    Posts
    65

    how to replace word with dot into underscore?

    Eg:
    word : ONLINE.job

    Need like : ONLINE_job

    I Know to use 'sed command for file'

    sed 's/ / /g' filename ................ but how to do for particular word?

  2. #2
    Member
    Join Date
    Sep 2012
    Posts
    63
    If you want to replace the dot with underscore in all the lines, then use the below sed command:

    Code:
    sed 's/\./_/g' input.dat
    To restrict for a particular line, then specify a pattern

    Code:
    sed '/ONLINE/ s/\./_/g' input.dat

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    63
    To restrict replacing for a particular word:

    Code:
    sed  's/\(ONLINE\)\.\(job\)/\1_\2/g' input.dat

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    CHENNAI
    Posts
    65
    thanks for your reply anitha....

    But ONLINE.JOB is just a example word. I have so many words like this. it will come in iteration way....

    Now var i has the word with dot operator..

    i = A.b (FIRST TIME)
    I=JJJ.B (2ND TIME)

    ........
    .......
    LIKE WISE IT IS GOING ON.

    So I Need general solution.... like word may be whatever ..... we need to change dot operator to underscore.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    63
    The following shell script might work for you:

    Code:
    #!/bin/bash
    
    SEARCH_WORD='ONLINE.job'
    SEARCH_WORD=`echo $SEARCH_WORD|sed 's/\./\\\\./g'`
    FILENAME='input.dat'
    REPLACE_WORD=`echo $SEARCH_WORD| sed 's/\./_/'`
    eval "sed 's/$SEARCH_WORD/$REPLACE_WORD/g' $FILENAME"
    Use the above code in a while loop for iterations.

Similar Threads

  1. Replies: 0
    Last Post: 01-02-2013, 06:48 PM
  2. Replies: 0
    Last Post: 12-13-2012, 10:47 PM
  3. Replacing a word in a text by another given word
    By suve in forum C, CPP Programs
    Replies: 1
    Last Post: 10-15-2012, 08:20 AM
  4. file path contain(dot) not support in QProcess
    By suve in forum Programming languages
    Replies: 1
    Last Post: 10-09-2012, 01:46 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •