[comp.unix.questions] Deleting

tkevans@fallst.UUCP (Tim Evans) (06/29/90)

Can someone help me to delete the _first_ occurring blank line of a file
(whereever it occurs) without deleting subsequent ones?

You'd think the following would work, but it doesn't:

	sed 's/^$//1' foo > newfoo

Solutions other than those using 'perl' are preferred.

Thanks
-- 
UUCP:		{rutgers|ames|uunet}!mimsy!woodb!fallst!tkevans
INTERNET:	tkevans%fallst@wb3ffv.ampr.org
Tim Evans	2201 Brookhaven Ct, Fallston, MD 21047

rostamia@umbc3.UMBC.EDU (Rouben Rostamian) (06/30/90)

In article <1651@fallst.UUCP> tkevans@fallst.UUCP (Tim Evans) writes:
>Can someone help me to delete the _first_ occurring blank line of a file
>(whereever it occurs) without deleting subsequent ones?

I do not know to do this in sed, but here's how it can be done in ed:

1. Create a file "mystrip" containing:
ed $1 << 'EOF'
/^$/d
w
q
'EOF'

2. chmod u+x mystrip

3. To strip the first blank line in file "try", do
   mystrip try

rostamia@umbc3.UMBC.EDU (Rouben Rostamian) (07/01/90)

In article <1651@fallst.UUCP> tkevans@fallst.UUCP (Tim Evans) writes:
>Can someone help me to delete the _first_ occurring blank line of a file
>(whereever it occurs) without deleting subsequent ones?

Here are two solutions using awk.  In both cases it is assumed that the
command is executed as a script, and $1 denotes the input file.

Solution 1:

awk < $1 'BEGIN{RS=""}
     { if (NR <= 2 )
           print $0;
       else
           print "\n" $0;
     }'

Solution 2:   Note: Here X denotes a character which does not occur in the
input file.  Replace with a more suitable character.  A control character,
such as ^A, may be a good choice.

awk < $1 'BEGIN{RS=""} {print $0; ORS=""; RS="X"}'

--

merlyn@iwarp.intel.com (Randal Schwartz) (07/01/90)

In article <1651@fallst.UUCP>, tkevans@fallst (Tim Evans) writes:
| Can someone help me to delete the _first_ occurring blank line of a file
| (whereever it occurs) without deleting subsequent ones?
| 
| You'd think the following would work, but it doesn't:
| 
| 	sed 's/^$//1' foo > newfoo
| 
| Solutions other than those using 'perl' are preferred.

But, since you didn't say *necessary*, how about:

perl -ne 'print unless /^$/ && $once++;'

Easy, easy, easy.

Just another Perl hacker,
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

leo@ehviea.ine.philips.nl (Leo de Wit) (07/01/90)

In article <1651@fallst.UUCP> tkevans@fallst.UUCP (Tim Evans) writes:
|Can someone help me to delete the _first_ occurring blank line of a file
|(whereever it occurs) without deleting subsequent ones?
|
|You'd think the following would work, but it doesn't:
|
|	sed 's/^$//1' foo > newfoo

Sure. It isn't valid sed syntax.

|
|Solutions other than those using 'perl' are preferred.

Here's a quick and dirty one with sed:

sed -n -e '
    /^$/{
        : back
        n
        p
        b back
    }
    p' foo >newfoo

All lines before the first empty line are printed by the last 'p' command.
Then, at the first empty line, each next line is read in and printed.

    Leo.

omerzu@quando.quantum.de (Thomas Omerzu) (07/02/90)

In article <819@ehviea.ine.philips.nl> leo@ehviea.UUCP (Leo de Wit) writes:

>In article <1651@fallst.UUCP> tkevans@fallst.UUCP (Tim Evans) writes:
>|Can someone help me to delete the _first_ occurring blank line of a file
>|(whereever it occurs) without deleting subsequent ones?
>|
>|You'd think the following would work, but it doesn't:
>|
>|	sed 's/^$//1' foo > newfoo
>
>Sure. It isn't valid sed syntax.


NO! It is valid sed syntax.

The reason, that this does not work as supposed is different:
the pattern  /^$/  matches  any empty line.
On the first occurency (indicated by '1'), this emptiness is replaced
by //, i.e. nothing, and the line is  p r i n t e d.
And this is the actual problem: /^$/ does  n o t  include the '\n',
which ist created by printing the empty line.


-- 
*-----------------------------------------------------------------------------*
Thomas Omerzu      UUCP:     ...!unido!quando!omerzu / omerzu@quando.uucp
  Quantum GmbH,    Bitnet:   UNIDO!quando!omerzu / omerzu%quando@UNIDO(.bitnet)
Dortmund, Germany  Internet: omerzu@quando.quantum.de

leo@ehviea.ine.philips.nl (Leo de Wit) (07/02/90)

In article <1607@quando.quantum.de> omerzu@quando.quantum.de (Thomas Omerzu) writes:
|
|In article <819@ehviea.ine.philips.nl> leo@ehviea.UUCP (Leo de Wit) writes:
|
|>In article <1651@fallst.UUCP> tkevans@fallst.UUCP (Tim Evans) writes:
|>|Can someone help me to delete the _first_ occurring blank line of a file
|>|(whereever it occurs) without deleting subsequent ones?
|>|
|>|You'd think the following would work, but it doesn't:
|>|
|>|	sed 's/^$//1' foo > newfoo
|>
|>Sure. It isn't valid sed syntax.
|
|
|NO! It is valid sed syntax.
|

(this is on Pyramid OSx):

Script started on Mon Jul  2 07:22:27 1990
ehviea_leo> sed 's/^$//1' foo > newfoo
command garbled: s/^$//1
ehviea_leo> exit
ehviea_leo> 
script done on Mon Jul  2 07:22:48 1990

So it is at least non-portable.

    Leo.