Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2008
    Posts
    7

    Unanswered: using sed in selected lines

    Hello everyone,
    Am writin a script with awk aiming to substitute a message in selected lines
    i dont know how to insert sed command in the if condition to replace only the selected lines

    awk /^VDD/'{

    getline;getline;v=split($2,tab,"1V8_2V5_3V3") ;

    if ($1=="|" && v!=1) {getline;getline; sed 's/ERROR/warning/g' file> tmp }


    }' file

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    sed is not an awk built-in function; it's a separate utility. Try using sub().
    ---
    "It does not work" is not a valid problem statement.

  3. #3
    Join Date
    Mar 2008
    Posts
    7
    Thank U for repliyin
    I've tried to use sub() but it doesent work
    i tried this
    awk /^VDD/'{

    getline;getline;v=split($2,tab,"1V8") ;

    if ($1=="|" && v!=1)
    {
    getline;getline; str=$0;
    sub (/ERROR/,"Warning",str); print str
    }
    }'

    (I want to use sub to make a modification only in the selected line)

  4. #4
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    Have you tried reading the awk manual? There are some examples showing how to properly use the sub() function.
    ---
    "It does not work" is not a valid problem statement.

  5. #5
    Join Date
    Mar 2008
    Posts
    7
    Thank U i have used the gsub in a gawk command and its OK
    but now I am trying to save the modif of selected lines in the source file not in a new file (I've tried to use print "$0" >> filename but it puts the new lines in the end of the file)
    gawk /^VDD/'{

    getline;getline;v=split($2,tab,"1V8_") ;if ($1=="|" && v!=1)

    {
    getline;getline;str=$0;gsub(/ERROR/,"warning",str) ;
    print str >> "checkSupply"
    }
    }'

  6. #6
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    You cannot do it with awk. You'll have to write to a new file, then rename. If you think you need to edit the file in place you should use something like ed or vi.
    ---
    "It does not work" is not a valid problem statement.

  7. #7
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    Rather than giving us your code, why not just give a quick description of what you want and a few lines of test data and what you would expect as output. It sounds like something than can be done by sed alone but I can't work out what you're trying to do by looking at your code.

  8. #8
    Join Date
    Mar 2008
    Posts
    7
    In fact I have a file which contains ERROR messages.Some of these messages are wrong So I must select these wrong messages and make a modif only on these lines. Now with my code I can select the lines but I cant make the modif on the source file . The file must stay the same Only selected lines have to be modified

  9. #9
    Join Date
    Mar 2008
    Posts
    7
    Problem solved
    Thank U 4 all

  10. #10
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    You would run something like the following. Seeing as you don't want to give a proper example I've made up my own text. You'll need a sed command for each message you want to change. I'll assume you know about the pipe character | etc.

    Code:
    cat OldErrorFile | sed 's/BadMsg1/GoodMsg1/g' | sed 's/BadMsg2/GoodMsg2/g' > NewErrorFile
    mv NewErrorFile OldErrorFile
    Mike

  11. #11
    Join Date
    Mar 2008
    Posts
    7
    Thanks for the idea I'll try to use it maybe to improve my code

Posting Permissions

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