SED SED SED...

SED SED SED...

Post by DW » Thu, 06 May 1993 07:27:02



Post follow-ups to comp.unix.questions, please.


| Is this simple or what: I want to erase the last 4 lines of a
| thousand files with variable length (I want to do more than
| that but I have succeded in the rest). I have tried $-4,$ d
| in sed (and many MANY variants of this) but it wont do.
| I have ended up using $ d (which works!!!) four times, but
| this makes my scripts long and the procces four times slower!!!

sed has no idea when it has reached $-4 or $-3, only when it has reached $.

Suggestion #1 -- if it must be done with sed, here goes:

sed '1N
     2N
     N
     $d
     P
     D'

Under GNU sed and possibly under BSD sed, it will require a -n flag to
operate properly on files of three or fewer lines, but one wouldn't run it
on files that short anyway.  -n won't hurt under System V, but it isn't
necessary, and no implementation of sed should need -n for files of four
lines or more.

Suggestion #2 -- forget sed:

ed - file << \FOOF
$-3,$ d
w
q
FOOF

[$-4,$ d would delete the last FIVE lines.  Ranges are inclusive.]

Suggestion #3 -- some perl fan will write it in perl.

David W. Tamkin   Box 59297   Northtown Station, Illinois  60659-0297

 
 
 

SED SED SED...

Post by Brian T » Thu, 06 May 1993 08:43:42



> Post follow-ups to comp.unix.questions, please.


> | Is this simple or what: I want to erase the last 4 lines of a
> | thousand files with variable length (I want to do more than
> | that but I have succeded in the rest).
[...]
> Suggestion #2 -- forget sed:

> ed - file << \FOOF
> $-3,$ d
> w
> q
> FOOF

    Anything wrong with this?

for IN in "filespec"
do
        LINE_4=`wc -l $IN | awk '{print $1-4}'`
        head -$LINE_4 < $IN > tmp ; mv tmp $IN
done
--



 
 
 

1. sed sed sed

On solaris 9 - why are there multiple copies of sed installed.  One in /usr/ucb
owned by the SUNWscpu third party package, /usr/xpg4/bin/sed by SUNWxcu4, and
one in /usr/bin owned by SUNWcsu.  I've looked on other Sun machines not
maintained by me and the same versions of sed are in the same place owned by the
same packages.

Why the multiple copies?

2. no bitmaps with mwm

3. . 2 sed FAQs, sed exes, sed cetras; URLs

4. Hard to make it work...

5. (sed 1q ; sed 2q) : no output from 2nd 'sed'

6. PPP Question

7. . sed script dc greg ubben sed stud

8. Source(s)

9. to all sed hackers - joining lines with sed

10. better sed s/\ a/\ A/g | sed s/\ b/\ B/g | ...

11. aix 3.2 - see sed run, run sed run

12. another tough sed problem: sed dependency

13. String manipulation error & how to use sed in a sed command?