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

Thread: How can I remove a line in a file?

  1. #1
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    How can I remove a line in a file?

    I'm trying to write a script to look for a line in a file and remove it. *I can find the line
    Code:
    grep <line> < filename>
    but what I want to do is remove that line from the text.

    For example, suppose I have the file [tt]alice.txt[/tt] and I want to remove the lines that start with the word "[tt]So[/tt]":
    [tt]#cat alice.txt
    Alice was beginning to get very tired of sitting by her sister on the bank,
    and of having nothing to do: once or twice she had peeped into the book her
    sister was reading, but it had no pictures or conversations in it, `and
    what is the use of a book,' thought Alice `without pictures or
    conversation?'

    So she was considering in her own mind (as well as she could, for the hot
    day made her feel very sleepy and stupid), whether the pleasure of making a
    daisy-chain would be worth the trouble of getting up and picking the
    daisies, when suddenly a White Rabbit with pink eyes ran close by her.
    #grep So alice.txt
    So she was considering in her own mind (as well as she could, for the hot
    #<remove the above line>
    Alice was beginning to get very tired of sitting by her sister on the bank,
    and of having nothing to do: once or twice she had peeped into the book her
    sister was reading, but it had no pictures or conversations in it, `and
    what is the use of a book,' thought Alice `without pictures or
    conversation?'

    day made her feel very sleepy and stupid), whether the pleasure of making a
    daisy-chain would be worth the trouble of getting up and picking the
    daisies, when suddenly a White Rabbit with pink eyes ran close by her.
    [/tt]
    Any ideas? *Thanks.

  2. #2
    Mentor
    Join Date
    Jun 2001
    Posts
    1,672

    Re: How can I remove a line in a file?

    This will do it:
    Code:
    perl -e "s/^So.*\n//g;" -pi alice.txt

  3. #3
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Re: How can I remove a line in a file?


    This will do it:
    Code:
    perl -e "s/^So.*\n//g;" -pi alice.txt
    Thanks. *It seems to do the trick. *Let me see if I have this right. *The stuff in quotes looks like a regexp. *[tt]^So[/tt] means the line that starts with "So", the period means any character and the asterisk means any number of the previous character (in this case, the period wildcard meaning any character). *The [tt]\n[/tt] looks like the escape character in c/c++/java for new line (or is that carrage return), so I'm guessing that's what it means in regexp, too. *So it says find a line that starts with "So" and ends with a carrage return. *I have no idea what [tt]s/[/tt] and [tt]//g[/tt] mean. *Thanks for the help.

  4. #4
    Mentor
    Join Date
    Jun 2001
    Posts
    1,672

    Re: How can I remove a line in a file?


    Thanks. It seems to do the trick. Let me see if I have this right. The stuff in quotes looks like a regexp. [tt]^So[/tt] means the line that starts with "So", the period means any character and the asterisk means any number of the previous character (in this case, the period wildcard meaning any character). The [tt]\n[/tt] looks like the escape character in c/c++/java for new line (or is that carrage return), so I'm guessing that's what it means in regexp, too. So it says find a line that starts with "So" and ends with a carrage return. I have no idea what [tt]s/[/tt] and [tt]//g[/tt] mean. Thanks for the help.
    Yep, you got it. s means replace instead of just find, g means global, so it won't stop after the first match.

  5. #5

    Re: How can I remove a line in a file?

    Perl is overkill:

    Code:
    grep -v ^So alice.txt

  6. #6
    Moderator
    Kind Mastermind
    stryder144's Avatar
    Join Date
    Aug 2001
    Location
    Denver, CO
    Posts
    2,925

    Re: How can I remove a line in a file?


    Perl is overkill:

    Code:
    grep -v ^So alice.txt
    I was wondering when you or GnuVince would give an example. Of course, I didn't expect you to use bash, I expected either perl from you or ruby/python from GV. Wow. I think that I will have to dig deep and read that bash link all the way through.

  7. #7
    Guest

    Re: How can I remove a line in a file?


    Perl is overkill:

    Code:
    grep -v ^So alice.txt
    You need to redirect.

  8. #8

    Re: How can I remove a line in a file?

    Of course, I didn't expect you to use bash, I expected either perl from you or ruby/python from GV.
    Use the right tool for the right job, I suppose. I think perl is mega overkill for something so simple as removing lines from a file.

    I think that I will have to dig deep and read that bash link all the way through.
    Bash scripts are more useful than you might think: they can use any binary that is installed on the system, such as grep, sed, and awk, and that makes bash a force to be reckoned with

    Vince is right; I do have to redirect. I didn't realize that the perl script altered the file instead of just outputting to stdout. Here is the new bash script:

    Code:
    grep -v ^So alice.txt > tmp; mv -f tmp alice.txt
    Redirecting back onto the same file will clobber the file; we must use a temp file instead. I suppose Perl might win this one if you have a deep-seated hatred for tempfiles.

  9. #9
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Re: How can I remove a line in a file?

    Redirecting back onto the same file will clobber the file; we must use a temp file instead. I suppose Perl might win this one if you have a deep-seated hatred for tempfiles.
    Also, the perl command just looks so much cooler

  10. #10
    Associate
    Join Date
    May 2001
    Posts
    19

    Re: How can I remove a line in a file?



    Also, the perl command just looks so much cooler
    sure, if sloppy == qool

    ;D

Similar Threads

  1. Adding a line to a file
    By rhoobler in forum Linux - General Topics
    Replies: 1
    Last Post: 02-12-2005, 07:15 PM
  2. Removing a line from a file
    By tolstoy in forum Linux - Software, Applications & Programming
    Replies: 3
    Last Post: 10-24-2002, 07:41 PM
  3. how do i remove a immutable file from reiserfs
    By LiNuts in forum Linux - General Topics
    Replies: 5
    Last Post: 07-05-2002, 05:16 PM
  4. How can i remove this file !
    By LiNuts in forum Linux - General Topics
    Replies: 6
    Last Post: 05-17-2002, 01:12 AM
  5. can't remove a core file
    By neo77777 in forum Linux - Software, Applications & Programming
    Replies: 4
    Last Post: 01-04-2002, 11:58 PM

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
  •