Results 1 to 8 of 8

Thread: Linux / Unix: sed find and replace

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Talking Linux / Unix: sed find and replace

    Hi,
    I need your urgent help :/

    I have the following file:

    root@ee [/]# cat a.txt
    /home/xx1/index.php
    /home/xx2/galeria.php

    sed command:

    root@ee [/]# sed -i 's|\(.*\)|<span style="color=red">\1</span>|g' a.txt
    root@ee [/]# cat a.txt
    <span style="color=red">/home/xx1/index.php</span>
    <span style="color=red">/home/xx1/index.php</span>

    but I need to be as follows:

    /home/xx1/index.php <span style="color=red">/home/xx1/index.php</span>
    /home/xx2/galeria.php <span style="color=red">/home/xx1/index.php</span>

    help please

  2. #2
    Senior Member
    Join Date
    Aug 2011
    Posts
    367
    Thanks
    0
    Thanked 55 Times in 51 Posts
    Rep Power
    7

    Default

    so what?
    repeat the back reference where ever you need it to be.
    «A problem clearly stated is a problem half solved.»

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Someone who can help me please?

  4. #4
    Senior Member Rahul.Patil's Avatar
    Join Date
    Feb 2012
    Location
    Mumbai india
    Posts
    447
    Thanks
    10
    Thanked 46 Times in 43 Posts
    Rep Power
    6

    Default

    Quote Originally Posted by javiercampos View Post
    Someone who can help me please?
    Hi,
    using sed:
    Code:
    sed -i.bkp 's|\(.*\)|\1 <span style="color=red">\1</span>|g'  /tmp/a.txt
    using awk:
    Code:
    awk '{ print $0,"<span style=\"color=red\">"$0"</span>"}'  /tmp/a.txt
    Rahul Patil <http://www.linuxian.com>

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default Hope helpful

    You may using & as the matched string
    sed 's|\(.*\)|&<span style="color=red">&</span>|g' a.txt
    Here is my output:
    /home/xx1/index.php <span style="color=red">/home/xx1/index.php</span>
    /home/xx2/galeria.php <span style="color=red">/home/xx2/galeria.php</span>


  6. #6
    Senior Member
    Join Date
    Jun 2007
    Location
    Hyderabad, AP, India
    Posts
    805
    Thanks
    44
    Thanked 55 Times in 48 Posts
    Rep Power
    11

    Default

    My two cents..

    sed -ri 's|(.*)|&<span style="color=red">&</span>|g' a.txt

    -r for regexp enabling so that you can just mention (.*) instead of \(.*\). And I given -i for editing a.txt file directly.
    Thanks,
    Surendra Kumar Anne
    Linux: Fast, friendly, flexible and .... free!
    Support Open source.
    http://www.linuxnix.com

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Quote Originally Posted by kumarat9pm View Post
    My two cents..

    sed -ri 's|(.*)|&<span style="color=red">&</span>|g' a.txt

    -r for regexp enabling so that you can just mention (.*) instead of \(.*\). And I given -i for editing a.txt file directly.
    To kumarat9pm : good solution,your method is more efficient. May I ask a question? Do you know what the \S means is the following commands? I google it ,but don't find the expected answer, thanks.
    echo "IPL 200 PARM CLEAR LOADPARM 0009" | sed 's/.*\sPARM\s\(\S\S*\).*/\1/p'

  8. #8
    Senior Member
    Join Date
    Jun 2007
    Location
    Hyderabad, AP, India
    Posts
    805
    Thanks
    44
    Thanked 55 Times in 48 Posts
    Rep Power
    11

    Default

    Quote Originally Posted by Learningshell View Post
    To kumarat9pm : good solution,your method is more efficient. May I ask a question? Do you know what the \S means is the following commands? I google it ,but don't find the expected answer, thanks.
    echo "IPL 200 PARM CLEAR LOADPARM 0009" | sed 's/.*\sPARM\s\(\S\S*\).*/\1/p'
    \S means.. match other than space, tabs etc..

    mention in other post which you asked..
    Sed command need your help,thanks in advance!
    Thanks,
    Surendra Kumar Anne
    Linux: Fast, friendly, flexible and .... free!
    Support Open source.
    http://www.linuxnix.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. script to find & replace IP's ina config file
    By kpit in forum Shell scripting
    Replies: 1
    Last Post: 25th May 2012, 08:22 PM
  2. Replies: 4
    Last Post: 11th May 2012, 12:14 PM
  3. [Solved] Linux / UNIX find command -printf syntax to print filename
    By vapornoob in forum Shell scripting
    Replies: 3
    Last Post: 4th November 2010, 05:23 PM
  4. To find and replace in text file
    By nashtech in forum Shell scripting
    Replies: 9
    Last Post: 4th November 2010, 05:05 PM
  5. Replies: 1
    Last Post: 11th May 2009, 11:09 PM

Tags for this Thread

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41