Results 1 to 3 of 3

Thread: Search string, put it into variable and remove line under sed / awk

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default Search string, put it into variable and remove line under sed / awk

    hi, need help, i am trying to create a script that search for string from file and put first line where the string found into variable

    so my file look like this :

    11-29-12 status 123: ->456
    11-28-12 status 123: ->321
    11-27-12 status 123: ->456


    Code:
    reslog=`cat filename`
    if [[ "$reslog" == "->456" ]]
            then
                variable1=  #i want put first line that contain "->456" into this variable, so variable1 will be= "11-29-12 status 123: ->456"
                # after that i want that line to be removed from file
    fi
    i don't know how to do this with awk, grep, or sed

  2. #2
    Senior Member
    Join Date
    Aug 2011
    Posts
    406
    Thanks
    0
    Thanked 57 Times in 53 Posts
    Rep Power
    9

    Default

    hi,

    from google: sed remove first line matching pattern
    Code:
    sed '0,/456/{//d}' yourFile
    done !

    that's all.
    A problem clearly stated is a problem half solved.

  3. The Following User Says Thank You to Watael For This Useful Post:

    initial (29th November 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Quote Originally Posted by Watael View Post
    hi,

    from google: sed remove first line matching pattern
    Code:
    sed '0,/456/{//d}' yourFile
    done !

    that's all.
    thanks, my bad, had been query google for some keywords but didn't give me fascinating result.


    i want to put conditional statement, if that statement is true, the line will be deleted, if it's not, it won't be deleted.
    i will try it myself. thanks again.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. remove line by line loop
    By chewku in forum Shell scripting
    Replies: 1
    Last Post: 18th September 2012, 09:37 AM
  2. Search for string and add a new line two lines down
    By hcclnoodles in forum Shell scripting
    Replies: 1
    Last Post: 1st August 2012, 06:20 PM
  3. Search a string in files
    By sandy_76 in forum Shell scripting
    Replies: 1
    Last Post: 28th November 2011, 05:25 PM
  4. Awk search and match string or word only
    By suvajit_sid in forum Shell scripting
    Replies: 13
    Last Post: 23rd June 2011, 05:57 PM
  5. How To Search String In Files?
    By ubu-user in forum Shell scripting
    Replies: 2
    Last Post: 18th April 2011, 10:58 PM

Tags for this Thread

Posting Permissions

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