hello all! I'd like to ask if there's a unix command that could delete
parts of the file given a string. Example:
Given: File1 which contains
--testing--
test123
--testing--
--hello--
world
--hello--
If my string is hello, the output would be another file or the same
file but without the hello block. So, the output would be
--testing--
test123
--testing--
if anybody knows please help! thanks!
|
|
0
|
|
|
|
Reply
|
wyl_lyf (55)
|
7/7/2003 8:51:38 PM |
|
In article <304f3217.0307071251.7293969a@posting.google.com>,
wyl_lyf@yahoo.com (melissa_benkyo) wrote:
> hello all! I'd like to ask if there's a unix command that could delete
> parts of the file given a string. Example:
> Given: File1 which contains
> --testing--
> test123
> --testing--
> --hello--
> world
> --hello--
>
> If my string is hello, the output would be another file or the same
> file but without the hello block. So, the output would be
> --testing--
> test123
> --testing--
>
> if anybody knows please help! thanks!
You could write a script in perl to exclude all lines which contain
"hello". Same with awk.
--
DeeDee, don't press that button! DeeDee! NO! Dee...
|
|
0
|
|
|
|
Reply
|
Michael
|
7/8/2003 12:29:29 AM
|
|
melissa_benkyo wrote:
>
> If my string is hello, the output would be another file or the same
> file but without the hello block.
It sounds like you want to eliminate a "block" of text, starting and ending
with a key word on a line of its own. That's not going to be accomplished
with a simple grep as has been suggested, but there are a few equally easy
ways of accomplishing this.
Before we go there, though....might this be a homework assignment that
you're hoping to accomplish today?
--
Reply address is a spam trap. Reply here only.
|
|
0
|
|
|
|
Reply
|
SW
|
7/8/2003 2:25:29 AM
|
|
In article <304f3217.0307071251.7293969a@posting.google.com>, melissa_benkyo wrote:
> hello all! I'd like to ask if there's a unix command that could delete
> parts of the file given a string. Example:
> Given: File1 which contains
> --testing--
> test123
> --testing--
> --hello--
> world
> --hello--
>
> If my string is hello, the output would be another file or the same
> file but without the hello block. So, the output would be
> --testing--
> test123
> --testing--
If you want to lose the --hello-- / --hello-- limited block, you could
try if sgrep (http://www.cs.helsinki.fi/u/jjaakkol/sgrep.html) does
what you want.
Taneli
|
|
0
|
|
|
|
Reply
|
Taneli
|
7/8/2003 8:25:47 AM
|
|
* melissa_benkyo <wyl_lyf@yahoo.com>:
> hello all! I'd like to ask if there's a unix command that could delete
> parts of the file given a string. Example:
> Given: File1 which contains
> --testing--
> test123
> --testing--
> --hello--
> world
> --hello--
>
> If my string is hello, the output would be another file or the same
> file but without the hello block. So, the output would be
> --testing--
> test123
> --testing--
I assume you want to delete all hello blocks including the "--hello--"
lines. Then
sed -e '/--hello--/,/--hello--/d' < infile > outfile
should suffice. If the command should modify the file rather than
writing a new one,
perl -i -ne 'print unless /--hello--/ ... /--hello--/' thefile
or
perl -0777 -i -pe 's/--hello--.*?--hello--\n//gs' thefile
can mimick that (Perl internally writes a new file and renames it).
-ooo- Andy
|
|
0
|
|
|
|
Reply
|
Andy
|
7/8/2003 9:17:56 AM
|
|
melissa_benkyo wrote:
> hello all! I'd like to ask if there's a unix command that could delete
> parts of the file given a string. Example:
> Given: File1 which contains
> --testing--
> test123
> --testing--
> --hello--
> world
> --hello--
>
> If my string is hello, the output would be another file or the same
> file but without the hello block. So, the output would be
> --testing--
> test123
> --testing--
>
> if anybody knows please help! thanks!
awk '
BEGIN { pr=1 }
{
if (pr==1){
if($0 ~ "--hello--") pr=0
else print
}
else if($0 ~ "--hello--") pr=1
}'
--
(c) ljm @ xs4all . nl. No part of this copyright message may be
reproduced, read or seen, dead or alive or by any means, including
but not limited to telepathy without the benevolence of the author.
|
|
0
|
|
|
|
Reply
|
Laurent
|
7/8/2003 2:00:01 PM
|
|
In article <1057697526.951081@smirk>,
James T. Dennis <jadestar@idiom.com> wrote:
....
> These awk scripts are just filters; you'd have to use redirection,
> temporary files and renaming (a shell script wrapper around these
> awk commands) to change your file. Alternatively you can use the
> perl -i (in situ edit) switch to force your changes directly into
> the file. I'll leave that (and concerns about robustness and error
> handling --- like if the perl script is killed abruptly in mid-file)
> as exercises to the original poster.
Note that Perl does *not* do in-situ editing. It is just pretend (aka,
syntactic sugar). Perl's -i switch is 100% identical to a wrapper around
an AWK script like this:
tmp=/tmp/tmp.$$
awk ... infile > $tmp
mv $tmp infile
(with, of course, the added proviso that -ibak leaves the original file
intact as foo.bak - again, easily enough implemented in a shell wrapper)
Note that the file systems of Unix and Unix-like systems (including
DOS/Windows) simply do not support in-situ editing - one way or another,
the file has to be re-written from scratch. Now, MTS, on the other hand...
Note also that using "ex" as a scripting utility comes closest (*), but
even then, a temp file is involved (it is kept very well hidden from the
user, however).
(*) And, incidentally, is a good solution to a lot of these Usenet queries,
like, e.g., "how do I delete the last 3 lines of a file?" Generally, "ex"
solutions are clearer and more robust than the typical sed/perl line noise.
|
|
0
|
|
|
|
Reply
|
gazelle
|
7/8/2003 10:09:45 PM
|
|
Kenny McCormack <gazelle@yin.interaccess.com> wrote:
> In article <1057697526.951081@smirk>,
> James T. Dennis <jadestar@idiom.com> wrote:
> ...
>> These awk scripts are just filters; you'd have to use redirection,
>> temporary files and renaming (a shell script wrapper around these
>> awk commands) to change your file. Alternatively you can use the
>> perl -i (in situ edit) switch to force your changes directly into
>> the file. I'll leave that (and concerns about robustness and error
>> handling --- like if the perl script is killed abruptly in mid-file)
>> as exercises to the original poster.
> Note that Perl does *not* do in-situ editing. It is just pretend (aka,
> syntactic sugar). Perl's -i switch is 100% identical to a wrapper around
> an AWK script like this:
> tmp=/tmp/tmp.$$
> awk ... infile > $tmp
> mv $tmp infile
> (with, of course, the added proviso that -ibak leaves the original file
> intact as foo.bak - again, easily enough implemented in a shell wrapper)
> Note that the file systems of Unix and Unix-like systems (including
> DOS/Windows) simply do not support in-situ editing - one way or another,
> the file has to be re-written from scratch. Now, MTS, on the other hand...
Slight misunderstanding seems to have slipped in. UN*X allows perfectly well
a process to read and write ( also known as "update" ) a file. There
is nothing strange about that.
--
Peter H�kanson
IPSec Sverige ( At Gothenburg Riverside )
Sorry about my e-mail address, but i'm trying to keep spam out,
remove "icke-reklam" if you feel for mailing me. Thanx.
|
|
0
|
|
|
|
Reply
|
phn
|
7/9/2003 7:22:02 AM
|
|
|
7 Replies
95 Views
(page loaded in 0.107 seconds)
|