Sed question

Polytropon freebsd at edvax.de
Mon Dec 22 04:11:31 UTC 2008


On Mon, 22 Dec 2008 05:31:08 +0200, Giorgos Keramidas <keramida at ceid.upatras.gr> wrote:
> On Sun, 21 Dec 2008 14:27:44 -0800, Gary Kline <kline at thought.org> wrote:
> > 	  perl -pi.bak -e 's/OLDSTRING/NEWSTRING/g' file1 file2 fileN
> >
> > 	  that i swiped somewhere.  [?]
> >
> > 	  last night i was up until the wee hours coding or extending
> > 	  a c++ program to assist in this stuff.  while i really get
> > 	  off on hacking code, it's less of a thrill at 02:10, say:_)
> 
> You don't need C++ for this.  If you don't mind the verbosity, Python
> can do the same thing with:
> 
>   #!/usr/bin/env python
> 
>   import sys
> 
>   skiplines = [1, 3]              # line numbers that should be skipped
>   lc = 0
>   for l in sys.stdin.readlines():
>       lc += 1
>       if not (lc in skiplines):
>           print l,
> 

Interesting example. The same could be achieved using awk:

	awk '(NR != 1 && NR != 3)' <sourcefile>

NR specifies the number of record (input line). But I still
think the sed in-place editing method is the most comfortable
one, allthough your example raises my interest in learning Python.



-- 
Polytropon
>From Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...


More information about the freebsd-questions mailing list