Find the answer to your Linux question:
Results 1 to 6 of 6
Hi all, I facing 2 problems: problem 1: How to grep lines ? I want to grep a line with 2 underscores in a file like this. Code: L1field1_L1field2_L1field3_L1field4 L2field1_L2field2_L2field3 ...
  1. #1
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Question Questions on sed and grep

    Hi all,
    I facing 2 problems:
    problem 1:
    How to grep lines ?
    I want to grep a line with 2 underscores in a file like this.
    Code:
    L1field1_L1field2_L1field3_L1field4
    L2field1_L2field2_L2field3
    L3field1_L3field2
    The line with two underscores,which is :
    L2field1_L2field2_L2field3

    problem 2:
    How to use sed with \ character ?
    I want to replace word called "word_123" into "word\123"
    replace _ with \ using sed.

    Cheers
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    To replace the underscore with a backslash in lines with 2 underscores:

    Code:
    awk 'BEGIN{FS="_"}NF==3{print $1 "\\" $2 "\\"$3;next}{print}'
    Regards

  3. #3
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    shell script
    Code:
    #!/bin/sh
    while read -r line
    do
     before=`printf $line|wc -c`
     after=`printf $line | tr -d '_' | wc -c`
     n=`echo "$before - $after" | bc`
     if [ $n -eq 2 ];then
        echo $line | sed 's/_/\\/g' 
     fi 
    done < "file"

  4. #4
    Linux Newbie
    Join Date
    Jan 2008
    Location
    UK
    Posts
    211
    Hello, out of interest unless you really need or want to use sed, Perl on the command line is very quick.
    at prompt write:
    perl -p -i -e 's/word_123/word\\123/g' then the file name

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by wowbag1 View Post
    Hello, out of interest unless you really need or want to use sed, Perl on the command line is very quick.
    at prompt write:
    perl -p -i -e 's/word_123/word\\123/g' then the file name
    The OP only wants to edit lines with 2 underscores.

    Regards

  6. #6
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Post

    Thanks guyz for your help
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

Posting Permissions

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