Find the answer to your Linux question:
Results 1 to 7 of 7
hi i got my project due after 4 hrs...i m just need a simple logic which doesnt seems to be commin to my mind... i want to find a word ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2005
    Posts
    6

    need help immeditely


    hi
    i got my project due after 4 hrs...i m just need a simple logic which doesnt seems to be commin to my mind...
    i want to find a word from a file and then i want to delete all the above lines
    before it..keeping the lines after that..
    that means
    if the word"go" is in line 4 of file..then i want all the lines after 7 on my new file
    e.g
    asassa
    asas
    asassa
    go
    u
    should
    be
    happy
    so in my new file it should hav
    u
    should
    be
    happy
    now i hav done cat file | grep -v "go"
    but then how do i get it....i m just too confused..help will be appreciated...
    abhargav

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    What I would do here (though a little convoluted) is to create a script that sets a flag when "go" is detected. Then, if the flag is true, you print the line.

    I don't know if it's ethical to give you the code (and I don't know BASH, only Perl), but you basically could probably read the correct lines into an array, then set your output to the file you're reading from, and recreate it.

    Just a hunch. Maybe I'll write it and give it a shot...

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Yeah, I just wrote a script that does this correctly.

    It's pretty simple. My Perl script is 31 lines long. So go ahead and give it a shot.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Apr 2005
    Posts
    6
    i m trying mate...wud it be possible...if u can post ur script i m doin bash so hope completely diffrent

  6. #5
    Linux Engineer
    Join Date
    Mar 2005
    Location
    Where my hat is
    Posts
    766
    You have read the FAQ and rules for the site haven't you? Homework questions are not allowed. Gotta do it on your own.
    Registered Linux user #384279
    Vector Linux SOHO 7

  7. #6
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    It can be done in just two lines.That is if you assume that the word
    you're looking for wil definitely be in the file , otherwise you need
    a few more lines to print an error message.

    So the first line of the script will put inside a variable the line number
    of the first line of the file which contains the word.For this you
    use grep , head and cut.
    The second line of the script will just use the tail command
    to print from that line (plus 1) onwards.

  8. #7
    Linux Guru
    Join Date
    Apr 2003
    Location
    London, UK
    Posts
    3,284
    locking topic, stop trying to get other people to do your homework for you.

Posting Permissions

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