Ask a Question related to Linux / Unix Administration, Design and Development.
-
melissa_benkyo #1
unix command to delete parts of a file
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!
melissa_benkyo Guest
-
Shell command from within Adobe Professional, copy and delete file
I was hoping one of the experienced technical folks on this forum could help me out. BACKGROUND - A group within our organization is responsible... -
what is different between SIZE and RES of unix command top?
Hi there, i saw the man page for top command SIZE : the total size of the process(text, data, and stack) RES : the current amount of resident... -
Create but not delete a file in unix ?
Hi, I want to ask a question about unix file permissions. We have a processs on our solaris box runs as user jboss. The user jboss creates a log... -
Tool to compare screenshots and delete all unchanged parts
If the 2 shots are actually identical, except for some small details, you could place them in 2 layers and set the blend mode of the top one to... -
Unix alias command in script file
Le 29/05/03 3:19, dans 0001HW.BAFAD84400017877F0305600@news.earthlink.net, « Dave Reiser » <dbreiser@earthlink.net> a écrit : Yet another option... -
SW #2
Re: unix command to delete parts of a file
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.
SW Guest
-
Taneli Vähäkangas #3
Re: unix command to delete parts of a file
In article <304f3217.0307071251.7293969a@posting.google.com >, melissa_benkyo wrote:
If you want to lose the --hello-- / --hello-- limited block, you could> 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--
try if sgrep ([url]http://www.cs.helsinki.fi/u/jjaakkol/sgrep.html[/url]) does
what you want.
Taneli
Taneli Vähäkangas Guest
-
Andy Zweck #4
Re: unix command to delete parts of a file
* melissa_benkyo <wyl_lyf@yahoo.com>:
I assume you want to delete all hello blocks including the "--hello--"> 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--
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
Andy Zweck Guest
-
Laurent-jan #5
Re: unix command to delete parts of a file
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.
Laurent-jan Guest
-
Kenny McCormack #6
Re: unix command to delete parts of a file
In article <1057697526.951081@smirk>,
James T. Dennis <jadestar@idiom.com> wrote:
....Note that Perl does *not* do in-situ editing. It is just pretend (aka,> 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.
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.
Kenny McCormack Guest
-
phn@icke-reklam.ipsec.nu #7
Re: unix command to delete parts of a file
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)Slight misunderstanding seems to have slipped in. UN*X allows perfectly well> 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...
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.
phn@icke-reklam.ipsec.nu Guest



Reply With Quote

