Results 1 to 10 of 10

Thread: To find and replace in text file

  1. #1
    Junior Member
    Join Date
    Nov 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default To find and replace in text file

    Hi,

    I need help on following problem.

    I have a text file something like below...

    I have to replace ":" "." "space" with , using either sed or awk can some one help me on this...

    Thanks in Advance..

    =====================================

    21:2007.11.13 00:38:13:399 Env/TP-Processor8:INFO:CORE BEGIN Session=9C3ACA5E8BE596EC39D2761AC66EC9E2.bstby1 Active Sessions: 776
    29:2007.11.13 00:38:13:918 Env/TP-Processor6:INFO:CORE BEGIN Session=3152E297D96912192109B8F9BEBB1446.bstby1 Active Sessions: 777
    88:2007.11.13 00:38:15:832 Env/TP-Processor2:INFO:CORE BEGIN Session=50D731F3EE59D3595A25140F166E9F93.bstby1 Active Sessions: 767
    116:2007.11.13 00:38:16:793 Env/TP-Processor7:INFO:CORE BEGIN Session=B7977F67D06591A78AA2F290DBCE6F2C.bstby1 Active Sessions: 768
    144:2007.11.13 00:38:19:219 Env/TP-Processor12:INFO:CORE BEGIN Session=19C3468F71BC6B2BEA03C3F2D4F74E44.bstby1 Active Sessions: 769
    172:2007.11.13 00:38:21:930 Env/TP-Processor2:INFO:CORE BEGIN Session=13DA23B1E5787E4E07B3098A17CAFBAC.bstby1 Active Sessions: 770


    =============================================

  2. #2
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,513
    Thanks
    17
    Thanked 804 Times in 511 Posts
    Rep Power
    10

    Default

    Code:
    echo '21:2007.11.13 00:38:13:399 Env/TP-Processor8:INFO:CORE BEGIN Session=9C3ACA5E8BE596EC39D2761AC66EC9E2.bstby1 Active Sessions: 776' | sed -e "s/:/./g"
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  3. #3
    Junior Member
    Join Date
    Nov 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Thanks a lot Vivek... It worked perfectly

  4. #4
    Junior Member
    Join Date
    Jul 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default i got the below answer but if i m hving tht txt in file..where to mention my filename

    i got the below answer but if i m hving tht txt in file..where is the place int the script to mention my filename sam.txt
    sam.txt contains
    21:2007.11.13 00:38:13:399 Env/TP-Processor8:INFO:CORE BEGIN Session=9C3ACA5E8BE596EC39D2761AC66EC9E2.bstby1 Active Sessions: 776
    29:2007.11.13 00:38:13:918 Env/TP-Processor6:INFO:CORE BEGIN Session=3152E297D96912192109B8F9BEBB1446.bstby1 Active Sessions: 777
    88:2007.11.13 00:38:15:832 Env/TP-Processor2:INFO:CORE BEGIN Session=50D731F3EE59D3595A25140F166E9F93.bstby1 Active Sessions: 767
    116:2007.11.13 00:38:16:793 Env/TP-Processor7:INFO:CORE BEGIN Session=B7977F67D06591A78AA2F290DBCE6F2C.bstby1 Active Sessions: 768
    144:2007.11.13 00:38:19:219 Env/TP-Processor12:INFO:CORE BEGIN Session=19C3468F71BC6B2BEA03C3F2D4F74E44.bstby1 Active Sessions: 769
    172:2007.11.13 00:38:21:930 Env/TP-Processor2:INFO:CORE BEGIN Session=13DA23B1E5787E4E07B3098A17CAFBAC.bstby1 Active Sessions: 770


    Quote Originally Posted by nashtech View Post
    Hi,

    I need help on following problem.

    I have a text file something like below...

    I have to replace ":" "." "space" with , using either sed or awk can some one help me on this...

    Thanks in Advance..

    =====================================

    21:2007.11.13 00:38:13:399 Env/TP-Processor8:INFO:CORE BEGIN Session=9C3ACA5E8BE596EC39D2761AC66EC9E2.bstby1 Active Sessions: 776
    29:2007.11.13 00:38:13:918 Env/TP-Processor6:INFO:CORE BEGIN Session=3152E297D96912192109B8F9BEBB1446.bstby1 Active Sessions: 777
    88:2007.11.13 00:38:15:832 Env/TP-Processor2:INFO:CORE BEGIN Session=50D731F3EE59D3595A25140F166E9F93.bstby1 Active Sessions: 767
    116:2007.11.13 00:38:16:793 Env/TP-Processor7:INFO:CORE BEGIN Session=B7977F67D06591A78AA2F290DBCE6F2C.bstby1 Active Sessions: 768
    144:2007.11.13 00:38:19:219 Env/TP-Processor12:INFO:CORE BEGIN Session=19C3468F71BC6B2BEA03C3F2D4F74E44.bstby1 Active Sessions: 769
    172:2007.11.13 00:38:21:930 Env/TP-Processor2:INFO:CORE BEGIN Session=13DA23B1E5787E4E07B3098A17CAFBAC.bstby1 Active Sessions: 770

    =============================================

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

    Default From Santosh P

    Open file in vi editor and do this :%s/:/.g

    Quote Originally Posted by pooppp View Post
    i got the below answer but if i m hving tht txt in file..where is the place int the script to mention my filename sam.txt
    sam.txt contains
    21:2007.11.13 00:38:13:399 Env/TP-Processor8:INFO:CORE BEGIN Session=9C3ACA5E8BE596EC39D2761AC66EC9E2.bstby1 Active Sessions: 776
    29:2007.11.13 00:38:13:918 Env/TP-Processor6:INFO:CORE BEGIN Session=3152E297D96912192109B8F9BEBB1446.bstby1 Active Sessions: 777
    88:2007.11.13 00:38:15:832 Env/TP-Processor2:INFO:CORE BEGIN Session=50D731F3EE59D3595A25140F166E9F93.bstby1 Active Sessions: 767
    116:2007.11.13 00:38:16:793 Env/TP-Processor7:INFO:CORE BEGIN Session=B7977F67D06591A78AA2F290DBCE6F2C.bstby1 Active Sessions: 768
    144:2007.11.13 00:38:19:219 Env/TP-Processor12:INFO:CORE BEGIN Session=19C3468F71BC6B2BEA03C3F2D4F74E44.bstby1 Active Sessions: 769
    172:2007.11.13 00:38:21:930 Env/TP-Processor2:INFO:CORE BEGIN Session=13DA23B1E5787E4E07B3098A17CAFBAC.bstby1 Active Sessions: 770

  6. #6
    Senior Member
    Join Date
    Sep 2006
    Posts
    131
    Thanks
    0
    Thanked 33 Times in 29 Posts
    Rep Power
    11

    Default

    you can just use the shell

    Code:
    while read -r line
    do
        echo ${line//[:. ]/,}
    done <"file" > new
    mv new file
    or you can use awk

    Code:
    awk '{gsub(/[:, ]/,","}1' file

  7. #7
    sukujgrg
    Guest

    Default

    I think this will help you.

    sed -i 's/[.,:, ]/ /g' filename
    ----
    Suku
    techtrunch.com
    Last edited by sukujgrg; 6th September 2010 at 11:05 AM.

  8. #8
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default howto replace text with a variable

    Hello,

    I'd like to do something like above, but instead of searching a specific text I'd like to use the variable a. How is the syntax in the second awk line for $a?


    I tryed this, but nothing works:

    HTML Code:
    a=`awk '{if ($1 == "###" && $2 == "END" ) { getline; print } } ' networking`
    
    b="mimi"
    awk -v var=$b '{ gsub(/[$a]/,var,$0); print }' networking > bla[/QUOTE]awk -v var=$b '{ gsub(/[$a]/,var,$0); print }' networking > bla
    HTML Code:
    awk -v var=$b '{ gsub(/$a/,var,$0); print }' networking > bla
    HTML Code:
    awk -v var=$b '{ gsub($a,var,$0); print }' networking > bla
    Or maybe in other words: in the /etc/init.d/networking file I want to write 3 lines below the text "### END INIT INFO".

    I should search for the line containing ### END INIT INFO and then write something below this line.
    Please help!
    edubidu

  9. #9
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,513
    Thanks
    17
    Thanked 804 Times in 511 Posts
    Rep Power
    10

    Default

    Quote Originally Posted by edubidu View Post
    Or maybe in other words: in the /etc/init.d/networking file I want to write 3 lines below the text "### END INIT INFO".

    I should search for the line containing ### END INIT INFO and then write something below this line.
    Please help!
    edubidu
    Try sed with append command (a )
    Code:
     sed '
    /### END INIT INFO/ a\
    One\
    Two\
    Three
    ' /etc/init.d/networking
    OR save to other file:
    Code:
    sed '
    /### END INIT INFO/ a\
    One\
    Two\
    Three
    ' /etc/init.d/networking  > /tmp/output
    Or pass sed -i option to update the file itself.
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  10. #10
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Quote Originally Posted by nixcraft View Post
    Try sed with append command (a )
    Code:
     sed '
    /### END INIT INFO/ a\
    One\
    Two\
    Three
    ' /etc/init.d/networking
    OR save to other file:
    Code:
    sed '
    /### END INIT INFO/ a\
    One\
    Two\
    Three
    ' /etc/init.d/networking  > /tmp/output
    Or pass sed -i option to update the file itself.
    cool, this works! THANK YOU!

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replacing text in a file using awk
    By postyrus in forum Shell scripting
    Replies: 6
    Last Post: 5th October 2011, 09:55 PM
  2. Cups is not printing text file with proper format, staircasing issue
    By Ashish Pathak in forum CentOS / RHEL / Fedora
    Replies: 1
    Last Post: 18th November 2007, 08:12 AM
  3. can not find the username in /etc/passwd file
    By subrata1in in forum Getting started tutorials
    Replies: 5
    Last Post: 27th March 2007, 11:45 AM
  4. can not find user name in /etc/passwd file
    By subrata1in in forum Linux software
    Replies: 2
    Last Post: 26th March 2007, 10:44 AM
  5. rearranging columns in a text file
    By sureshbup in forum Shell scripting
    Replies: 2
    Last Post: 6th December 2006, 09:43 AM

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
  •