Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2007
    Posts
    11

    Unanswered: Help needed in a shell script

    Hi,

    I am try to remove certain lines from a file

    here is what i need :

    File1 .txt

    aaa
    bbb
    bbb

    wewe
    wreq
    1234
    11111
    1111
    13324
    2332434
    1343434

    i am searching for a string 1234 once that string is found i need to remove 1234 and the lines which are above (2 lines and 5 lines down ) these are constant for my work

    So the output file should be only like this ::
    =====================================
    aaa
    bbb
    bbb

  2. #2
    Join Date
    May 2007
    Location
    Milano, Italy
    Posts
    22
    If you have GNU grep:

    Code:
    grep -v "$(grep -B2 -A5 1234 yourfile)" yourfile

  3. #3
    Join Date
    Sep 2007
    Posts
    11
    i donot have a gnu grep any other option ??

    Thanks

  4. #4
    Join Date
    May 2007
    Location
    Milano, Italy
    Posts
    22
    If your file is not big:

    Code:
    awk '{x[NR]=$0}
    	/1234/{y[NR]}END{
    		for(j in y)
    			for(i=j-2;i<j+6;i++)
    				delete x[i]
    		for(i=1;i<=NR;i++)
    			if(i in x)
    	print x[i]
    }' file
    If your awk doesn't support the delete statement:

    Code:
    awk '{x[NR]=$0}
    	/1234/{y[NR]}END{
    		for(j in y)
    			for(i=j-2;i<j+6;i++)
    				x[i]="not_this_one"
    		for(i=1;i<=NR;i++)
    			if(x[i]!="not_this_one")
    	print x[i]
    }' file

  5. #5
    Join Date
    Sep 2007
    Posts
    11
    Thanks It worked .. Thanks a lot !!!!

Posting Permissions

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