unix command to delete parts of a file

Ask a Question related to Linux / Unix Administration, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default 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

  4. #3

    Default Re: unix command to delete parts of a file

    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 ([url]http://www.cs.helsinki.fi/u/jjaakkol/sgrep.html[/url]) does
    what you want.

    Taneli

    Taneli Vähäkangas Guest

  5. #4

    Default Re: unix command to delete parts of a file

    * 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
    Andy Zweck Guest

  6. #5

    Default 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

  7. #6

    Default Re: unix command to delete parts of a file

    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.
    Kenny McCormack Guest

  8. #7

    Default 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)
    > 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.
    phn@icke-reklam.ipsec.nu Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139