unix command to delete parts of a file

unix command to delete parts of a file

Post by melissa_benk » Wed, 09 Jul 2003 05:51:38



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!

 
 
 

unix command to delete parts of a file

Post by Len Philp » Wed, 09 Jul 2003 09:34:59



> 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--

$ grep -v hello File1 > anotherFile

You can fine tune the expression (the argument following the -v option)
to be more selective in what's matched or not. 'man grep' will give you
more options. For example, on my Red Hat box, to match '--hello--', it
takes :

$ grep -v '\--hello--' File1

which returns :

--testing--
test123
--testing--
world

There are probably a gazillion ways to do this, including using sed, but
I'm not near as fluent with sed as I should be.

--

-- Len Philpot                          ><>  --



 
 
 

unix command to delete parts of a file

Post by SW » Wed, 09 Jul 2003 11:25:29



> 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.

 
 
 

unix command to delete parts of a file

Post by Taneli V?h?kanga » Wed, 09 Jul 2003 17:25:47



> 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

 
 
 

unix command to delete parts of a file

Post by Andy Zwec » Wed, 09 Jul 2003 18:17:56



Quote:> 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

 
 
 

unix command to delete parts of a file

Post by Laurent-ja » Wed, 09 Jul 2003 23:00:01



> 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

Quote:}'

--

reproduced, read or seen, dead or alive or by any means, including
but not limited to telepathy  without the benevolence of the author.
 
 
 

unix command to delete parts of a file

Post by James T. Denni » Thu, 10 Jul 2003 05:52:07



> 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!

 Well, if the block begin and end markers were different you could
 use a simple awk one-liner:

        awk '/--hello-begin--/,/--hello-end--/ {next}; 1'

 For lines between the -begin and -end pattern, skip to the next line.
 The trailing ;1 in the awk script is a shorthand for {print} (1 is
 "true" and the default action for any condition that holds true in awk
 is {print}).  This is a case of brevity and idiom over clarity.

 The "pattern,pattern" idiom in awk is obscure but very old.

 The problem here is that the begin and end are the same pattern.  So
 we have to build a mini-state machine; toggling a flag off when we're
 skipping text and on when we're printing it.

        awk 'BEGIN {f=1};/^--hello--$/ {f++; next}; f%2'

 ... this is almost the same length.  First we set the flag on; so
 we start printing.  We check for our toggle.  This time I made the
 regex more specific, forcing it to match the whole line, in the
 previous example I was deliberately less specific --- you should
 adapt the regex to your needs.  When we see our marker we simple
 increment the flag and skip to the next line (so we aren't printing the
 *closing* toggle line later).  f%2 is a condition that is true whenever
 f divided by two leaves a remainder --- i.e. for all odd values of f.
 As in my previous example, a true condition in awk implicitly does a
 {print}.

 So, in answer to your question --- no, there isn't a utility to do this.
 However, the scripting utilities in UNIX are more than adequate to perform
 this in a single line.  This can be done, albeit differently in sed.
 A Perl version of his would be trivial as well.

 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.

--
Jim Dennis,
Starshine: Signed, Sealed, Delivered

 
 
 

unix command to delete parts of a file

Post by James T. Denni » Thu, 10 Jul 2003 05:58:07




>> 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
> }'

 This is a more verbose (far more readable) implementation of the
 mini state machine I described.  However, it's very on-awk like
 (awk-ward?)

 Here's a more customary verbose version (untested):

 #!/usr/bin/awk -f
 BEGIN { pr=1 }
 /--hello--/ {
        if (pr==1) {
                pr = 0
                }
        else {
                pr =1
                next
                }
        }
 pr==1 {print}

--
Jim Dennis,
Starshine: Signed, Sealed, Delivered

 
 
 

unix command to delete parts of a file

Post by Kenny McCorma » Thu, 10 Jul 2003 07:09:45




...

Quote:> 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.

 
 
 

unix command to delete parts of a file

Post by p.. » Thu, 10 Jul 2003 16:22:02





> ...
>> 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.

 
 
 

1. unix command to delete parts of a file

hi all! I have a question. is there a unix command that could crop a
file based on a search entry. for example:
file1 contains
--testing--
test
--testing--
--hello world--
hi there
test 123
--hello world--

then I'd like the output to be a file with the following entries:
--testing--
test
--testin--

How do I remove the --hello world-- whole block using unix commands?
currently what I'm doing is reading the file line by line and then
echoing the line to another file if the line isn't --hello world--. I
think this is not effective. So I'm looking for other alternatives.

thanks so much!

melissa

2. installp bombs with sysck: 3001-017 but without single usable hint

3. 'delete' command in ftp delete files in local hard disk ???

4. How to start POP3 ?

5. Can I find a part part number from unix command line?

6. Courier-I modem and PPP configuration?

7. Would sparse file technique help in deleting initial part of Accounting file?

8. X graphics performance vs. Windows performance

9. command file, delete the command after execution

10. replace part of a file with part of another file

11. A way to undelete (part) of a deleted file?

12. How to delete parts of strings in a file?

13. CAN I FTP FILES FROM UNIX TO NOVELL SERVERS USING THE UNIX COMMAND LINE???