Results 1 to 6 of 6

Thread: sed command matching multiple line pattern

  1. #1
    Join Date
    Apr 2010
    Beans
    4

    sed command matching multiple line pattern

    Hello all,

    Im trying to write a bash function to find and replace several lines in a very long text file. I started to use sed, but after wasting all day trying to get this to work, I have decided to post here. basically i'm looking for following pattern
    $1
    {
    type fixedValue;
    value uniform (* * *);
    }
    and replace with the following in the same file
    $1
    {
    type fixedValue;
    value uniform ($2 $3 $4);
    }

    where $1 $2 $3 $4 are arguments passed to the function. so far the function starts like:

    function changeValue {
    printf "Changing $1 value to ($2 $3 $4)\n"

    sed -i "/inlet \n { \n type fixedValue; \n value uniform/ s/(.*)/$1\n {\n type fixedValue;\n value uniform ($2 $3 $4);/g" U

    }

    if I have it only match the word inlet and paste in the rest of the lines, it works. However matching multiple lines is the problem. Also, the words uniform, fixedValue, type, and value could appear multiple times in the file. I'm new to sed (thats the real problem) and none of the tutorials I've found can really help. Any help is much appreciated.

  2. #2
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: sed command matching multiple line pattern

    sed normally works on one line at a time. If you want to work on several lines in the same command, you have to add them to the pattern buffer with the N command:

    Code:
    firas@aoba:~$ cat foo.txt 
    foo
    bar
    baz
    firas@aoba:~$ sed 's/foo\nbar/foobar/' foo.txt # does not work
    foo
    bar
    baz
    firas@aoba:~$ sed 'N;s/foo\nbar/foobar/' foo.txt # works
    foobar
    baz
    firas@aoba:~$ sed 'N;s/foo\nbar/goo\nbar/' foo.txt # also works
    goo
    bar
    baz
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: sed command matching multiple line pattern

    Hi, something like this may get you started:
    Code:
    sed "/${value1}/{N;N;N;N;s/${value1}\n{\ntype fixedValue;\nvalue uniform (\* \* \*)\;\n}/changedValue\n{\ntype fixedValue;\nvalue uniform(${value2} ${value3} ${value4});\n}/}" somefile
    if there is any leading / trailing whitespace on these lines then it will require some tweaking.

  4. #4
    Join Date
    Apr 2010
    Beans
    4

    Re: sed command matching multiple line pattern

    Thanks Bachstelze and DaithiF for the posts, both helped a lot. I ended up doing something like

    sed -i "/$1/{N; N; N; N; s/$1\n\ {\n\ type fixedValue;\n\ value uniform ([0-9] [0-9] [0-9]);/$1\n {\n type fixedValue;\n value uniform ($2 $3 $4);/g}" filename

    I had to put spaces after the "N;" and add in the tweeks for leading white spaces, along with matching the exact spacing between the value and uniform words (exact spacing did not show up in post here). However, I am having difficulty in finding/implementing a wildcard for numbers in those places I had stars (or the [0-9]'s now). The numbers could be positive or negative decimals with leading zeros and many decimal places. How is one able to search for any numbers in the parentheses after "uniform" and replace them with $2 $3 $4 values? Thanks again for your help.

  5. #5
    Join Date
    Apr 2010
    Beans
    4

    Re: sed command matching multiple line pattern

    I think i might have figured it out (quick testing says yes).

    replace
    uniform ([0-9] [0-9] [0-9])

    with
    uniform (*.*[0-9]* *.*[0-9]* *.*[0-9]*)

    and this seems to take care of positive and nagative numbers with decimal places and even numbers like -1.3434e-5

    I'll do more testing and post if this doesn't work.

  6. #6
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: sed command matching multiple line pattern

    or:
    -\?[0-9.]\+

Tags for this Thread

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
  •