This will do it:
Code:perl -e "s/^So.*\n//g;" -pi alice.txt
I'm trying to write a script to look for a line in a file and remove it. *I can find the line
but what I want to do is remove that line from the text.Code:grep <line> < filename>
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]":
Any ideas? *Thanks.[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]
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.This will do it:
Code:perl -e "s/^So.*\n//g;" -pi alice.txt
Yep, you got it. s means replace instead of just find, g means global, so it won't stop after the first match.
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.
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.Perl is overkill:
Code:grep -v ^So alice.txt
You need to redirect.Perl is overkill:
Code:grep -v ^So alice.txt
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.Of course, I didn't expect you to use bash, I expected either perl from you or ruby/python from GV.
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 withI think that I will have to dig deep and read that bash link all the way through.
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:
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.Code:grep -v ^So alice.txt > tmp; mv -f tmp alice.txt
Also, the perl command just looks so much coolerRedirecting 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.![]()
Bookmarks