Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Find pattern, delete lines below and above

  1. #1
    Join Date
    Aug 2012
    Beans
    9

    Find pattern, delete lines below and above

    Hi,

    I am totally new with scripting and could really use help from someone more experienced. I have to create a bash script that would look in particular folder, identify files that contain "P_SGSN_SESSION_MANAGEMENT" in filename. For every one of those files it should find pattern "PAPU_INDEX 36" and delete that pattern (which is in one line) and delete 42 lines above and 144 lines below. Here is part of one file:

    MNC 3
    SUCC_DEACT_SE_GGSN_RESET 0
    FAIL_PDP_ACT_DUE_NO_RESP 0
    PAPU_INDEX 36
    SUCC_IMPL_DEACT_SE_COLLISIONS 0

    I have found another topic about this
    HTML Code:
    http://ubuntuforums.org/showthread.php?t=1897438
    , but couldn't tweak it to fit to what I want.

    Thanks,
    Srdjan

  2. #2
    Join Date
    Aug 2008
    Location
    Sweden
    Beans
    303
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Find pattern, delete lines below and above

    This is my signature

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Find pattern, delete lines below and above

    Quote Originally Posted by Srdjan01 View Post
    Hi,

    I have found another topic about this
    HTML Code:
    http://ubuntuforums.org/showthread.php?t=1897438
    , but couldn't tweak it to fit to what I want.
    Code:
    ed -s input.txt <<< $'g/PAPU_INDEX/-42,+144d\n,p'
    did't work?
    Last edited by Vaphell; August 6th, 2012 at 10:26 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  4. #4
    Join Date
    Apr 2012
    Beans
    6,755

    Re: Find pattern, delete lines below and above

    modifying Peteris Krumins' one-liner #75 ('delete last 10 lines of file')

    Code:
    sed -e :a -e '$d;N;2,10ba' -e 'P;D'
    http://www.catonmat.net/blog/sed-one...ed-part-three/

    slightly, could you do

    Code:
    sed -e :a -e '/pattern/,+144d;N;2,43ba' -e 'P;D' yourfile
    (basically it maintains a moving buffer of 43 lines in the pattern space, and deletes it plus the next 144 lines if it matches pattern)

  5. #5
    Join Date
    Aug 2012
    Beans
    9

    Re: Find pattern, delete lines below and above

    Thanks everyone for your responses, much appreciated.

    There are basically 2 different problems here. First one is altering one file by removing the pattern. Code

    ed -s input.txt <<< $'g/PAPU_INDEX 36/-42,+144d\n,p' > output.txt

    worked fine. Thanks Vaphell.

    Secondly, I have to put in a script that can automatically apply changes to numerous files that all have "P_SGSN_SESSION_MANAGEMENT" in file name. When i was applying simple sed that script looked like this

    #!/bin/sh
    . /etc/profile
    /usr/bin/perl -e "
    s/ MGW_NAME 401/ MGW_NAME MNS01/g;
    s/ MGW_NAME 402/ MGW_NAME MNS02/g;
    s/ MGW_NAME 403/ MGW_NAME MNS03/g;
    s/ MGW_NAME 404/ MGW_NAME MNS04/g" -pi $(find /appl/virtuo/gways/spool/nokia/MGW/U4.1/temp/ -type f)
    mv /appl/virtuo/gways/spool/nokia/MGW/U4.1/temp/* /appl/virtuo/gways/spool/nokia/MGW/U4.1/output_d/

    Command ed -s input.txt <<< $'g/PAPU_INDEX 36/-42,+144d\n,p' prints result to a screen. Is it possible to apply change and return in to the same file? Something similar to this s/ MGW_NAME 401/ MGW_NAME MNS01/g;.

    Thanks,
    Srdjan

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Find pattern, delete lines below and above

    Code:
    s/ MGW_NAME 401/ MGW_NAME MNS01/g;
    s/ MGW_NAME 402/ MGW_NAME MNS02/g;
    s/ MGW_NAME 403/ MGW_NAME MNS03/g;
    s/ MGW_NAME 404/ MGW_NAME MNS04/g"
    this can be simplified to a variant of 's/( MGW_NAME )4(0[1-4])/\1MNS\2/g'


    Code:
    while read -r -d$'\0' file
    do
      ed -s "$file" <<< 'g/PAPU_INDEX 36/-42,+144d
                         ,s/\( MGW_NAME \)4\(0[1-4]\)/\1MNS\2/g
                         w' 
    done < <( find /appl/virtuo/gways/spool/nokia/MGW/U4.1/temp/ -iname '*P_SGSN_SESSION_MANAGEMENT*' -type f -print0 )
    is there a reason to run the script with sh and not bash? Bash in general is more feature rich.


    PS. damn it, there is next to no ed know-how on the internet, pretty much only dry man pages -_-
    Last edited by Vaphell; August 7th, 2012 at 12:44 PM. Reason: spotted typos in code snippet
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  7. #7
    Join Date
    Aug 2012
    Beans
    9

    Re: Find pattern, delete lines below and above

    Quote Originally Posted by Vaphell View Post
    is there a reason to run the script with sh and not bash? Bash in general is more feature rich.
    Not really. Will use bash from now on.

    I will test this out and let you know how it went.

    Thanks so much, I am really grateful for this.

    BR,
    Srdjan

  8. #8
    Join Date
    Aug 2012
    Beans
    9

    Re: Find pattern, delete lines below and above

    I have tried using this, it didn't delete the pattern.

    Code:
    #!/bin/bash
    . /etc/profile
    /usr/bin/perl -e "
    s/    SGSN_ID 223572/    SGSN_ID SBZ01/g;
    s/    SGSN_ID 223571/    SGSN_ID SBG01/g" -pi $(find /appl/virtuo/gways/config/ericsson-hlr/sg7/test/temp/ -type f)
    while read -r -d$'\0' file
    do
      ed -s "$file" <<< 'g/PAPU_INDEX 36/-42,+144d
                         w'
    done < <( find /appl/virtuo/gways/config/ericsson-hlr/sg7/test/temp/ -iname '*P_SGSN_SESSION_MANAGEMENT*' -type f -print0 )
    mv /appl/virtuo/gways/config/ericsson-hlr/sg7/test/temp/* /appl/virtuo/gways/config/ericsson-hlr/sg7/test/output_d/

  9. #9
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Find pattern, delete lines below and above

    my bad
    Code:
    while read -r -d $'\0' file
    do
      echo 'g/PAPU_INDEX 36/-42,+144d
            w' | ed -s "$file"
    done < <( find /appl/virtuo/gways/config/ericsson-hlr/sg7/test/temp/ -iname '*P_SGSN_SESSION_MANAGEMENT*' -type f -print0 )
    <<< "string" inside while loop doesn't seem work too well (probably ed gobbled up all input or sometghing - no wonder nobody uses it anymore, it's pita ), so i replaced it with piped echo.


    Code:
    -pi $(find /appl/virtuo/gways/config/ericsson-hlr/sg7/test/temp/ -type f)
    i guess there are no spaces in filenames - it would blow up spectacularly if there were. Are these files scattered in different directories that you have to resort to find?
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  10. #10
    Join Date
    Aug 2012
    Beans
    9

    Re: Find pattern, delete lines below and above

    Quote Originally Posted by Vaphell View Post
    Are these files scattered in different directories that you have to resort to find?
    Files do not have spaces in their names and they are placed in one folder. Script should apply changes and move files to a different folder.

    I'll try to test test this tonight and get back to you.

    Many thanks,
    Srdjan

Page 1 of 2 12 LastLast

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
  •