A general sed question

2009-10-07 Thread David Allen
I keep bumping up against this, so I thought I'd throw this question out
to those who understand sed better than I do.

What I'm trying to do is to clean up the contents of some files
(/sys/i386/conf/GENERIC would be a good example) to get more readable
diffs.  To that end, I'm trying to use sed to

 - delete commented lines
 - remove inline comments
 - remove trailing spaces and/or tabs
 - delete blank lines, and/or lines containing just spaces and/or tabs
 - expand tabs

Thanks!
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: A general sed question

2009-10-07 Thread Nerius Landys
Uh, I know I'm stating the obvious, but you might try these 2
techniques to enhance your diff experience:

1. Use diff -w.
2. Do cat filename | sort  filename.sorted for both files you are
diffing, and then compare both sorted files.
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: A general sed question

2009-10-07 Thread Oliver Fromme
David Allen the.real.david.al...@gmail.com wrote:
  I keep bumping up against this, so I thought I'd throw this question out
  to those who understand sed better than I do.
  
  What I'm trying to do is to clean up the contents of some files
  (/sys/i386/conf/GENERIC would be a good example) to get more readable
  diffs.  To that end, I'm trying to use sed to
  
   - delete commented lines
   - remove inline comments
   - remove trailing spaces and/or tabs
   - delete blank lines, and/or lines containing just spaces and/or tabs
   - expand tabs

I recommend to use the -Bb options of diff.  They cause diff
to ignore blank lines and any changes in the amount of white
space (including tabs).  You can also use -w to ignore *all*
white space, but note that foo bar and foobar are then
considered equal, which might not be what you want.

So only the removal of comments remains:

sed 's/#.*//'

That will remove all comments.  Afterwards, commented lines
are empty, so the -B option of diff will ignore them, so you
don't have to remove them explicitly.

When using zsh as your shell, you can use a nice feature
called process substitution, so you don't have to create
temporary files:

diff -Buw (sed 's/#.*//' GENERIC) (sed 's/#.*//' MYKERNEL)

I think bash has a similar feature, but I don't know the
syntax, so please see the manpage if you're a bash user.

If you need to do that oftem, it's worth to create an alias
or shell function.

Best regards
   Oliver

-- 
Oliver Fromme, secnetix GmbH  Co. KG, Marktplatz 29, 85567 Grafing b. M.
Handelsregister: Registergericht Muenchen, HRA 74606,  Geschäftsfuehrung:
secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün-
chen, HRB 125758,  Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart

FreeBSD-Dienstleistungen, -Produkte und mehr:  http://www.secnetix.de/bsd

People still program in C.  People keep writing shell scripts.  *Most*
people don't realize the shortcomings of the tools they are using because
they a) don't reflect on their workflows and they are b) too lazy to check
out alternatives to realize there is help. -- Simon 'corecode' Schubert
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: A general sed question

2009-10-07 Thread George Davidovich
On Tue, Oct 06, 2009 at 11:45:36PM -0700, David Allen wrote:
 I keep bumping up against this, so I thought I'd throw this question out
 to those who understand sed better than I do.
 
 What I'm trying to do is to clean up the contents of some files
 (/sys/i386/conf/GENERIC would be a good example) to get more readable
 diffs.  To that end, I'm trying to use sed to

For the following note that what's contained in the square brackets is a
space character followed by a literal TAB character (typically created
by entering ^V followed by TAB).

  - delete commented lines
  - remove inline comments

s/[ ]*#.*//   # takes care of both, but will leave \t\t\t\n

  - remove trailing spaces and/or tabs

s/[ ]*$// # handy, but not needed if using diff -b

  - delete blank lines, and/or lines containing just spaces and/or tabs

/^[ ]*$/d

  - expand tabs

This is overly complex with sed and probably unecessary.  Instead I'd
suggest using your editor (in vim, it's ':set expandtab | retab'), or
for interactive use, relying on expand(1) and using a value for -t that
matches the tab spacing you typically use for your pager and/or editor.
Alternatively, to get better visual alignment when using diff(1), just
use the -t option.

Putting the above together, you get

sed -e 's/[ ]*#.*//' -e 's/[]*$//' -e '/^[  ]*$/d'  

Hardly ideal but it's readable enough and satisfies the 80/20 rule.  If
used as a simple alias, shell function or script as Oliver Fromme
suggested (yes, this works in bash), my suggestion is

diff -ubBt (cleanup /sys/i386/conf/GENERIC) (cleanup /path/to/NEWKERNEL)

-- 
George
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: Sed question

2008-12-23 Thread Gary Kline
On Mon, Dec 22, 2008 at 08:53:36AM +, Matthew Seaman wrote:
 Gary Kline wrote:
 
  anyway, this is one for giiorgos, or another perl wiz. i've
  been using the perl subsitution cmd one-liner for years with
  unfailing success.  is there a way of deleting lines with perl
  using the same idea as:
 
perl -pi.bak -e 's/OLDSTRING/NEWSTRING/g' file1 file2 fileN
 
 To delete lines matching a R.E. (grep -v effectively):
 
perl -ni.bak -e 'm/SOMETHING/ || print;' file1 file2 fileN
 

Matthew, 

I've been trying, unsuccessfully, to parse the above.  What does
the m [in 'm/SOMETHING/' do.  i thought it was 'match'  ... and
another one, just FWIW: can perl's regex be set to ignore cases?


 To delete lines by number from many files -- eg. exclude lines 3 to 7:
 
perl -ni.bak -e 'print unless ( 3 .. 7 ); close ARGV if eof;' \
   file1 file2 fileN
 
 The malarkey with 'close ARGV' is necessary because otherwise perl
 won't reset the input line number counter ($.) for each new file.


yeah, it's pretty important to reset the counter to zero since
i've got so many files.

one way to avoid that extended line would be to embed the perl
string within a /bin/shell script, :-)  Scripts within scripts,
eh? lol.  Oh: a final question.  does the perl regex match vi's
/\foo\ ?  it seemed like this plot in /OLDSTRING/ failed last
sunday.  i'm not entirely sure, tho.

thanks much,

gary



 The range expression ( N .. M ) can take matching terms rather than
 line numbers, so you can also do things like:
 
perl -ni.bak -e 'print unless ( m/FIRST/ .. m/SECOND/ )' \
file1 file2 fileN
 
   Cheers,
 
   Matthew
 
 -- 
 Dr Matthew J Seaman MA, D.Phil.   7 Priory Courtyard
  Flat 3
 PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate
  Kent, CT11 9PW
 



-- 
 Gary Kline  kl...@thought.org  http://www.thought.org  Public Service Unix
http://jottings.thought.org   http://transfinite.thought.org
The 2.17a release of Jottings: http://jottings.thought.org/index.php

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: Sed question

2008-12-23 Thread Matthew Seaman

Gary Kline wrote:

On Mon, Dec 22, 2008 at 08:53:36AM +, Matthew Seaman wrote:

Gary Kline wrote:


anyway, this is one for giiorgos, or another perl wiz. i've
been using the perl subsitution cmd one-liner for years with
unfailing success.  is there a way of deleting lines with perl
using the same idea as:

  perl -pi.bak -e 's/OLDSTRING/NEWSTRING/g' file1 file2 fileN

To delete lines matching a R.E. (grep -v effectively):

   perl -ni.bak -e 'm/SOMETHING/ || print;' file1 file2 fileN



	Matthew, 


I've been trying, unsuccessfully, to parse the above.  What does
the m [in 'm/SOMETHING/' do.  i thought it was 'match'  ... and
another one, just FWIW: can perl's regex be set to ignore cases?


It's the standard perl regular expression matching operator.  See the
section on Regexp Quote-Like Operators in the perlop(1) man page.  What
is does is quite similar in English to the way it's written in perl: it
checks each line, and either matches SOMETHING or prints the line.

Perl RE's can do just about anything (including some things that are
actually impossible with pure Regular Expressions (like counting opening
and closing brackets)).  To make the match case insensitive just use:

   m/SOMETHING/i

(There are several other ways to achieve the same effect: this is perl, after
all)




To delete lines by number from many files -- eg. exclude lines 3 to 7:

   perl -ni.bak -e 'print unless ( 3 .. 7 ); close ARGV if eof;' \
file1 file2 fileN

The malarkey with 'close ARGV' is necessary because otherwise perl
won't reset the input line number counter ($.) for each new file.



yeah, it's pretty important to reset the counter to zero since
i've got so many files.

one way to avoid that extended line would be to embed the perl
string within a /bin/shell script, :-)  Scripts within scripts,
eh? lol.  Oh: a final question.  does the perl regex match vi's
/\foo\ ?  it seemed like this plot in /OLDSTRING/ failed last
sunday.  i'm not entirely sure, tho.


No need to do that.  perl is not just for one-liners, and it can be used as
the interpreter on shebang lines.  In fact, that's probably the most common way
of running perl stuff.  You can write the above (in a more long-winded way) as:

#!/usr/bin/perl -n

print unless ( 3 .. 7 );
close ARGV if eof;

Save as a file 'foo.pl', make it executable by chmod +x foo.pl and then just
run it as:

   ./foo.pl file1 file2 fileN

I'm not sure what \foo\ means in vi(1), but I'll hazard a guess that you're 
trying to match word boundaries.  Sure perl can do that.  You want the '\b'

thingy[*], like so: m/\bfoo\b/  which will match 'foo' as a separate word.  See
perlre(1) for the gory details (but be warned, it's a long read).

I did see another answer to your question by Jonathan McKeown who was actually a
lot more thorough about how to do this than I was.  He discussed the important
point about what happens if eg. you apply the 'delete lines 3 .. 7' command to a
file with only 6 lines, and more importantly how to stop that ruining your whole
day.

Cheers,

Matthew



thanks much,

gary







The range expression ( N .. M ) can take matching terms rather than
line numbers, so you can also do things like:

   perl -ni.bak -e 'print unless ( m/FIRST/ .. m/SECOND/ )' \
   file1 file2 fileN

Cheers,

Matthew

--
Dr Matthew J Seaman MA, D.Phil.   7 Priory Courtyard
 Flat 3
PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate
 Kent, CT11 9PW







[*] Described as a 'zero width assertion' in the man page, but that's too
much typing.

--
Dr Matthew J Seaman MA, D.Phil.   7 Priory Courtyard
 Flat 3
PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate
 Kent, CT11 9PW



signature.asc
Description: OpenPGP digital signature


Re: Sed question

2008-12-22 Thread Jonathan McKeown
On Monday 22 December 2008 00:27:44 Gary Kline wrote:

   anyway, this is one for giiorgos, or another perl wiz. i've
   been using the perl subsitution cmd one-liner for years with
   unfailing success.  is there a way of deleting lines with perl
   using the same idea as:

 perl -pi.bak -e 's/OLDSTRING/NEWSTRING/g' file1 file2 fileN

For a single file it's very easy:

perl -ne 'print unless 8..10' filename

will print every line except lines 8, 9 and 10.

The .. or range operator (in scalar context) is a sort of flip-flop. It keeps 
its own state, which is either true or false. When it's false it only 
evaluates its left-hand argument; when it's true it only evaluates its 
right-hand argument; and whenever the argument it's currently looking at 
returns true, the expression changes state.

If the argument is an integer, it's treated as a comparison against the 
current line number, $. ; so the first expression, 8..10, means

($. == 8) .. ($. == 10)

It's false to start with, until ($. == 8) returns true (on line 8); it becomes 
true and remains true until ($. == 10) returns true (on line 10), when it 
becomes false again and remains false until it next sees line number 8.

You can also use more complicated tests in the range operator:

perl -ne 'print unless /START/ .. /END/'

will find each line containing the word START anywhere, and delete from that 
line to the next line containing END (inclusive of both endpoints) - this 
will work for multiple occurrences of START and END in your file.

There are two problems if you string multiple files together on the command 
line: first, if you're using line numbers, the line number doesn't reset 
between files unless you do an explicit close on each file.

The bigger problem is if you have a file in which the second condition doesn't 
occur (a file with only 9 lines in the first example, or a file with a START 
and no corresponding END in the second case): the range operator will stay 
true until it sees the ending condition in the next file, meaning you'll lose 
the first ten lines in the numeric case, or every line from the top of file 
to the first END in the second case.

To get round these two problems, you need to test for eof in the range 
operator, and close each file when it hits eof to reset the line count.

perl -ne 'print unless 8 .. $. == 10 || eof; close ARGV if eof' file[1-n]
perl -ne 'print unless /START/../END/ || eof; close ARGV if eof' file[1-n]

There's some hairy precedence in the first range expression: a useful tip for 
checking that you've got it right (and indeed in general for checking that a 
bit of Perl does what you think it does) is the B::Deparse core module, which 
you call like this:

perl -MO=Deparse,-p -e 'print unless 8 .. $. == 10 || eof'

which outputs

((8 .. (($. == 10) || eof)) or print($_));
-e syntax OK

The ,-p argument to -MO=Deparse tells it to put in parentheses everywhere. If 
you're like me and like to leave them all out, feed your expression to 
Deparse with all the parens in and leave off the ,-p argument: Deparse will 
get rid of all the unnecessary ones:

$ perl -MO=Deparse -e 'print unless (8 .. (($. == 10) or eof))'
print $_ unless 8 .. $. == 10 || eof;
-e syntax OK

Jonathan
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: Sed question

2008-12-22 Thread Matthew Seaman

Gary Kline wrote:


anyway, this is one for giiorgos, or another perl wiz. i've
been using the perl subsitution cmd one-liner for years with
unfailing success.  is there a way of deleting lines with perl
using the same idea as:

  perl -pi.bak -e 's/OLDSTRING/NEWSTRING/g' file1 file2 fileN


To delete lines matching a R.E. (grep -v effectively):

   perl -ni.bak -e 'm/SOMETHING/ || print;' file1 file2 fileN

To delete lines by number from many files -- eg. exclude lines 3 to 7:

   perl -ni.bak -e 'print unless ( 3 .. 7 ); close ARGV if eof;' \
file1 file2 fileN

The malarkey with 'close ARGV' is necessary because otherwise perl
won't reset the input line number counter ($.) for each new file.
The range expression ( N .. M ) can take matching terms rather than
line numbers, so you can also do things like:

   perl -ni.bak -e 'print unless ( m/FIRST/ .. m/SECOND/ )' \
   file1 file2 fileN

Cheers,

Matthew

--
Dr Matthew J Seaman MA, D.Phil.   7 Priory Courtyard
 Flat 3
PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate
 Kent, CT11 9PW



signature.asc
Description: OpenPGP digital signature


Re: Sed question

2008-12-21 Thread Gary Kline
On Sun, 2008-12-21 at 07:42 +0200, Giorgos Keramidas wrote:
 On Sat, 20 Dec 2008 21:34:10 -0800, Gary Kline kl...@thought.org wrote:
  how can i delete, say, lines 8,9,and 10 from 200 files
  using sed?  Is it
 
  sed '8,10d' file newfile
  or is there a better way?
 
 Use in-place editing:
 
   keram...@kobe:/tmp$ cat -n foo
1  foo
2  bar
3  baz
   keram...@kobe:/tmp$ sed -i '' -e '2d' foo
   keram...@kobe:/tmp$ cat -n foo
1  foo
2  baz
   keram...@kobe:/tmp$
 
 Look at the manpage of sed for more details about the -i option, and
 consider using backup files while you are running tests.  In-place
 editing is very cool, but it can also make changes that are difficult
 to recover from.
 
thanks much.  it works just fine in-place.  ...but i did made a separate
copy, just in case;-)


___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: Sed question

2008-12-21 Thread Bertram Scharpf
Hi,

Am Sonntag, 21. Dez 2008, 02:08:04 -0800 schrieb Gary Kline:
 On Sun, 2008-12-21 at 07:42 +0200, Giorgos Keramidas wrote:
  On Sat, 20 Dec 2008 21:34:10 -0800, Gary Kline kl...@thought.org wrote:
  
   sed '8,10d' file newfile
   or is there a better way?
  
keram...@kobe:/tmp$ sed -i '' -e '2d' foo
  
 thanks much.  it works just fine in-place.  ...but i did made a separate
 copy, just in case;-)

To make a copy, call

  $ sed -i .bak 8,10d myfile

Be aware that the -i option is not portable. - man sed /^STANDARDS

Consider Perl or

  $ ruby -i.bak -pe 'next if 8..10 === $.' x

Bertram


-- 
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: Sed question

2008-12-21 Thread Gary Kline
On Sun, Dec 21, 2008 at 03:06:58PM +0100, Bertram Scharpf wrote:
 Hi,
 
 Am Sonntag, 21. Dez 2008, 02:08:04 -0800 schrieb Gary Kline:
  On Sun, 2008-12-21 at 07:42 +0200, Giorgos Keramidas wrote:
   On Sat, 20 Dec 2008 21:34:10 -0800, Gary Kline kl...@thought.org wrote:
   
sed '8,10d' file newfile
or is there a better way?
   
 keram...@kobe:/tmp$ sed -i '' -e '2d' foo
   
  thanks much.  it works just fine in-place.  ...but i did made a separate
  copy, just in case;-)
 
 To make a copy, call
 
   $ sed -i .bak 8,10d myfile
 
 Be aware that the -i option is not portable. - man sed /^STANDARDS


yeah, i've been checking around: sed, even gsed.  surprised
that the berkeley sed is non-standard with some flag.  [next
thing i'll read is that berkeley did Not code half of unix.]

anyway, this is one for giiorgos, or another perl wiz. i've
been using the perl subsitution cmd one-liner for years with
unfailing success.  is there a way of deleting lines with perl
using the same idea as:

  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:_)

  gary


 
 Consider Perl or
 
   $ ruby -i.bak -pe 'next if 8..10 === $.' x
 
 Bertram
 
 
 -- 
 Bertram Scharpf
 Stuttgart, Deutschland/Germany
 http://www.bertram-scharpf.de

-- 
 Gary Kline  kl...@thought.org  http://www.thought.org  Public Service Unix
http://jottings.thought.org   http://transfinite.thought.org
The 2.17a release of Jottings: http://jottings.thought.org/index.php

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: Sed question

2008-12-21 Thread Giorgos Keramidas
On Sun, 21 Dec 2008 14:27:44 -0800, Gary Kline kl...@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,

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: Sed question

2008-12-21 Thread Polytropon
On Mon, 22 Dec 2008 05:31:08 +0200, Giorgos Keramidas 
keram...@ceid.upatras.gr wrote:
 On Sun, 21 Dec 2008 14:27:44 -0800, Gary Kline kl...@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, ...
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Sed question

2008-12-20 Thread Gary Kline
how can i delete, say, lines 8,9,and 10 from 200 files
using sed?  Is it

sed '8,10d' file newfile
or is there a better way?

tia,

gary



-- 
 Gary Kline  kl...@thought.org  http://www.thought.org  Public Service Unix
http://jottings.thought.org   http://transfinite.thought.org
The 2.17a release of Jottings: http://jottings.thought.org/index.php

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: Sed question

2008-12-20 Thread Giorgos Keramidas
On Sat, 20 Dec 2008 21:34:10 -0800, Gary Kline kl...@thought.org wrote:
 how can i delete, say, lines 8,9,and 10 from 200 files
 using sed?  Is it

 sed '8,10d' file newfile
 or is there a better way?

Use in-place editing:

  keram...@kobe:/tmp$ cat -n foo
   1  foo
   2  bar
   3  baz
  keram...@kobe:/tmp$ sed -i '' -e '2d' foo
  keram...@kobe:/tmp$ cat -n foo
   1  foo
   2  baz
  keram...@kobe:/tmp$

Look at the manpage of sed for more details about the -i option, and
consider using backup files while you are running tests.  In-place
editing is very cool, but it can also make changes that are difficult
to recover from.

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: Sed question

2008-12-20 Thread prad
On Sat, 20 Dec 2008 21:34:10 -0800
Gary Kline kl...@thought.org wrote:

 or is there a better way?

nothing specific to add for your particular issue, but this link may
be useful in the future for sed:
http://sed.sourceforge.net/grabbag/tutorials/

-- 
In friendship,
prad

  ... with you on your journey
Towards Freedom
http://www.towardsfreedom.com (website)
Information, Inspiration, Imagination - truly a site for soaring I's
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: Sed question

2008-12-20 Thread Corey Chandler

Gary Kline wrote:

how can i delete, say, lines 8,9,and 10 from 200 files
using sed?  Is it

sed '8,10d' file newfile
or is there a better way?

  

I'd stick it in a for loop using inplace editing, but yes. :-)

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


A sed question

2008-07-09 Thread Unga
Hi all

I want to translate following GNU sed lines to FreeBSD sed:

1. sed -e '/\*address:/{n;[EMAIL PROTECTED]@replaceText @}'

2. sed -e '/\*address:/{n;[EMAIL PROTECTED]@ [EMAIL PROTECTED]'

Appreciate if someone could help with it.

Is there a good documentation about FreeBSD sed other than man page?

Kind regards
Unga


  
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: A sed question

2008-07-09 Thread Bertram Scharpf
Hii,

Am Mittwoch, 09. Jul 2008, 00:35:32 -0700 schrieb Unga:
 I want to translate following GNU sed lines to FreeBSD sed:
 
 1. sed -e '/\*address:/{n;[EMAIL PROTECTED]@replaceText @}'
 2. sed -e '/\*address:/{n;[EMAIL PROTECTED]@ [EMAIL PROTECTED]'

An obvious problem is that a semicolon is missing before the closing
brace.

sed -e '/\*address:/{n;[EMAIL PROTECTED]@replaceText @;}'

I'm almost sure your version won't work with GNU sed either.

Bertram


-- 
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: A sed question

2008-07-09 Thread Unga
--- On Wed, 7/9/08, Bertram Scharpf [EMAIL PROTECTED] wrote:

 From: Bertram Scharpf [EMAIL PROTECTED]
 Subject: Re: A sed question
 To: Unga [EMAIL PROTECTED], freebsd-questions@freebsd.org
 Date: Wednesday, July 9, 2008, 6:52 PM
 Hii,
 
 Am Mittwoch, 09. Jul 2008, 00:35:32 -0700 schrieb Unga:
  I want to translate following GNU sed lines to FreeBSD
 sed:
  
  1. sed -e '/\*address:/{n;[EMAIL PROTECTED]@replaceText
 @}'
  2. sed -e '/\*address:/{n;[EMAIL PROTECTED]@
 [EMAIL PROTECTED]'
 
 An obvious problem is that a semicolon is missing before
 the closing
 brace.
 
 sed -e '/\*address:/{n;[EMAIL PROTECTED]@replaceText
 @;}'
 
 I'm almost sure your version won't work with GNU
 sed either.
 

Thank you very much for the reply.

That was indeed the difference between the GNU sed and the FreeBSD sed in this 
case. I tested with a semicolon, it worked perfectly but the GNU sed does not 
require similar semicolon.

Best regards
Unga


  
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sed question...

2007-09-25 Thread Nikos Vassiliadis
On Tuesday 25 September 2007 06:07, Howard Goldstein wrote:
 Gary Kline wrote:
  My earlier post about deleting the first N lines was answered by
  this one-liner site {below}.   I wasn't including any
  redirection; doing so finally resolved the problem.  Now I need
  to delete every line from the 19th or so to the last line.

sed -e 18q
that is, quit after processing line 18.

  Question one, can anybody explain the following syntax?  What do
  P, D ba represent, in other words?

The manual page explains sed in a very good way. For sure, better
than I could describe it here. You'd better read it.

 
 
   # delete the last 10 lines of a file
   sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1
   sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2
 
 
  Question two, can sed do its thing inline?

Yes.
 -i extension
 Edit files in-place, saving backups with the specified extension.
 If a zero-length extension is given, no backup will be saved.  It
 is not recommended to give a zero-length extension when in-place
 editing files, as you risk corruption or partial content in situ-
 ations where disk space is exhausted, etc.


 Wouldn't it be easier to use  head -n 18 ?

No, it's the same. Some sed operation are trivial to read/write,
others aren't.

HTH

Nikos
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sed question...

2007-09-25 Thread Giorgos Keramidas
On 2007-09-24 20:52, Gary Kline [EMAIL PROTECTED] wrote:
On Mon, Sep 24, 2007 at 11:07:20PM -0400, Howard Goldstein wrote:
  # delete the last 10 lines of a file
  sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1
  sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2
 
 Question two, can sed do its thing inline?
 
 Wouldn't it be easier to use  head -n 18 ?

If you _know_ that the file has 28 lines, yes.  If you don't,
then itmay be tricky to 'guess' that -n 18 is the right option.

 No, because most of these files are between 40 and 50 lines.  I only
 care about the first 30 or 40; everything below has to be deleted.  By
 hand, using vi, I might type :31,$d that fixes that one file.  Of
 course, I could simply edit in 19 for 10 above.  It would be more
 savvy to understand the sed syntax.

You don't need to manually edit files with vi(1) if all you want to do
is type ``:31,$dRET:wqRET'' ...

sed -i '' -e '31,$d' file.txt

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sed question...

2007-09-25 Thread Gary Kline
On Tue, Sep 25, 2007 at 09:31:04AM +0300, Nikos Vassiliadis wrote:
 On Tuesday 25 September 2007 06:07, Howard Goldstein wrote:
  Gary Kline wrote:
 My earlier post about deleting the first N lines was answered by
 this one-liner site {below}.   I wasn't including any
 redirection; doing so finally resolved the problem.  Now I need
 to delete every line from the 19th or so to the last line.
 
 sed -e 18q
 that is, quit after processing line 18.


This quits after line 18, as you say.  Given a file of 100 lines,
I was everything from line 81,100d.  Which is what #method 1
does.  But trying to parse this from man sed is more than
difficule.  And I have yet to find ba in the man page.  That is
why I asked for some insights rather that to be told to go read
the man page; to me, that's dismissing the issue rather than
addressing it.

 
 Question one, can anybody explain the following syntax?  What do
 P, D ba represent, in other words?
 
 The manual page explains sed in a very good way. For sure, better
 than I could describe it here. You'd better read it.
 
  
  
# delete the last 10 lines of a file
sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1
sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2
  
  
 Question two, can sed do its thing inline?
 
 Yes.
  -i extension
  Edit files in-place, saving backups with the specified extension.
  If a zero-length extension is given, no backup will be saved.  It
  is not recommended to give a zero-length extension when in-place
  editing files, as you risk corruption or partial content in situ-
  ations where disk space is exhausted, etc.
 

Right.  I always do a perl -pi.bak [...] mostly out of habit.
With sed, redirection saved the new output, leaving the original
in ``.''  FWIW, I was using the sed on my Ubuntu server.  It is 
different from the BSD sed that I've used now/then since 1978.
The linux sed man page is just slightly more readable that the
BSD.  Probably newer.

gary


 
  Wouldn't it be easier to use  head -n 18 ?
 
 No, it's the same. Some sed operation are trivial to read/write,
 others aren't.
 
 HTH
 
 Nikos

-- 
  Gary Kline  [EMAIL PROTECTED]   www.thought.org  Public Service Unix
  http://jottings.thought.org   http://transfinite.thought.org

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sed question...

2007-09-25 Thread Howard Goldstein


Giorgos Keramidas wrote:
 On 2007-09-24 20:52, Gary Kline [EMAIL PROTECTED] wrote:
   
 On Mon, Sep 24, 2007 at 11:07:20PM -0400, Howard Goldstein wrote:
 
  # delete the last 10 lines of a file
  sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1
  sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2

Question two, can sed do its thing inline?
 
 Wouldn't it be easier to use  head -n 18 ?
   

 If you _know_ that the file has 28 lines, yes.  If you don't,
 then itmay be tricky to 'guess' that -n 18 is the right option.

   
In the bits you snipped he said he wanted to ditch everything after the
18th line. 

I didn't realize complexity was the goal.  Is this a homework assignment
Gary?
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sed question...

2007-09-25 Thread Gary Kline
On Tue, Sep 25, 2007 at 07:24:25PM +0300, Giorgos Keramidas wrote:
 On 2007-09-24 20:52, Gary Kline [EMAIL PROTECTED] wrote:
 On Mon, Sep 24, 2007 at 11:07:20PM -0400, Howard Goldstein wrote:
   # delete the last 10 lines of a file
   sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1
   sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2
  
Question two, can sed do its thing inline?
  
  Wouldn't it be easier to use  head -n 18 ?
 
 If you _know_ that the file has 28 lines, yes.  If you don't,
 then itmay be tricky to 'guess' that -n 18 is the right option.
 
  No, because most of these files are between 40 and 50 lines.  I only
  care about the first 30 or 40; everything below has to be deleted.  By
  hand, using vi, I might type :31,$d that fixes that one file.  Of
  course, I could simply edit in 19 for 10 above.  It would be more
  savvy to understand the sed syntax.
 
 You don't need to manually edit files with vi(1) if all you want to do
 is type ``:31,$dRET:wqRET'' ...
 
   sed -i '' -e '31,$d' file.txt


The catch is that I don't always know the linecount; the only
thing I have found--by examing ALL hundreds of files (briefly:)
--is that I was to delete the last 19 lines.  

If sed can understand negative indexing, then would -e '-19,$d'
work?  That would makr sense from a human standpoint; not sure
how that would fit the sed model, tho.

gary


 

-- 
  Gary Kline  [EMAIL PROTECTED]   www.thought.org  Public Service Unix
  http://jottings.thought.org   http://transfinite.thought.org

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sed question...

2007-09-25 Thread Gary Kline
On Tue, Sep 25, 2007 at 01:05:06PM -0400, Howard Goldstein wrote:
 
 
 Giorgos Keramidas wrote:
  On 2007-09-24 20:52, Gary Kline [EMAIL PROTECTED] wrote:

  On Mon, Sep 24, 2007 at 11:07:20PM -0400, Howard Goldstein wrote:
  
   # delete the last 10 lines of a file
   sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1
   sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2
 
   Question two, can sed do its thing inline?
  
  Wouldn't it be easier to use  head -n 18 ?

 
  If you _know_ that the file has 28 lines, yes.  If you don't,
  then itmay be tricky to 'guess' that -n 18 is the right option.
 

 In the bits you snipped he said he wanted to ditch everything after the
 18th line. 

Not 'xactly.  Given a file of, say 200 line, I want to exact 
just the middle.  Roughly everythiing after #155, and from the 
end, -19 to the EOF.

 
 I didn't realize complexity was the goal.  Is this a homework assignment
 Gary?


Errm, only working toward one, Howard.
-- 
  Gary Kline  [EMAIL PROTECTED]   www.thought.org  Public Service Unix
  http://jottings.thought.org   http://transfinite.thought.org

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sed question...

2007-09-25 Thread Nikos Vassiliadis
On Tuesday 25 September 2007 19:58, Gary Kline wrote:
   But trying to parse this from man sed is more than
   difficule.  And I have yet to find ba in the man page.  That is
   why I asked for some insights rather that to be told to go read
   the man page; to me, that's dismissing the issue rather than
   addressing it.

Hm, my suggestion was wrong... sed is a bit cryptic to learn from the
manual page. The manual can be used as a reference, if you alread know
sed.

So, ba is:
branch to the a label
for example:
my_label:
 ...
bmy_label

My points are:
If you want to learn sed, you have to invest some time.
If you want to ask for an one-liner that does what you want,
that's fine too.

sed isn't only s/foo/bar/. There is a dc(1) clone in sed,
which is at least an amazing accomplishment for sed. You
might want (or not) to learn more things about it.

   Right.  I always do a perl -pi.bak [...] mostly out of habit.
   With sed, redirection saved the new output, leaving the original
   in ``.''  FWIW, I was using the sed on my Ubuntu server.  It is
   different from the BSD sed that I've used now/then since 1978.

I think this version of sed is different too, since it's part
of BSD since 4.4BSD. It may be compatible with the older BSD
one. The in-place replacement is a non-standard extension which
both BSD and GNU sed share.

Cheers

Nikos
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sed question...

2007-09-25 Thread Nikos Vassiliadis
Gary,

 This will probably help you, it has many nice one-liners.

http://sed.sourceforge.net/grabbag/tutorials/sed1line.txt

Nikos
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sed question...

2007-09-25 Thread Gary Kline
On Tue, Sep 25, 2007 at 08:38:50PM +0300, Nikos Vassiliadis wrote:
 On Tuesday 25 September 2007 19:58, Gary Kline wrote:
  But trying to parse this from man sed is more than
  difficule.  And I have yet to find ba in the man page.  That is
  why I asked for some insights rather that to be told to go read
  the man page; to me, that's dismissing the issue rather than
  addressing it.
 
 Hm, my suggestion was wrong... sed is a bit cryptic to learn from the
 manual page. The manual can be used as a reference, if you alread know
 sed.
 
 So, ba is:
 branch to the a label
 for example:
 my_label:
  ...
 bmy_label


Thank you!  This will help me de-code that sed one-liner that was
evidently written by a sedexpert. The linux pages help further,
but I've found some soild tutorials.  
 
 My points are:
 If you want to learn sed, you have to invest some time.
 If you want to ask for an one-liner that does what you want,
 that's fine too.
 
 sed isn't only s/foo/bar/. There is a dc(1) clone in sed,
 which is at least an amazing accomplishment for sed. You
 might want (or not) to learn more things about it.
 
  Right.  I always do a perl -pi.bak [...] mostly out of habit.
  With sed, redirection saved the new output, leaving the original
  in ``.''  FWIW, I was using the sed on my Ubuntu server.  It is
  different from the BSD sed that I've used now/then since 1978.
 
 I think this version of sed is different too, since it's part
 of BSD since 4.4BSD. It may be compatible with the older BSD
 one. The in-place replacement is a non-standard extension which
 both BSD and GNU sed share.
 


The last time I used sed seriously was in the early 80's; then
there was only redirection :-)  much obliged for your tips.  I've
got to write a shell/sed script to edit the rest of this stuff.
Time to invest!!

gary


 Cheers
 
 Nikos
 ___
 freebsd-questions@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/freebsd-questions
 To unsubscribe, send any mail to [EMAIL PROTECTED]

-- 
  Gary Kline  [EMAIL PROTECTED]   www.thought.org  Public Service Unix
  http://jottings.thought.org   http://transfinite.thought.org

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sed question...

2007-09-25 Thread Gary Kline
On Tue, Sep 25, 2007 at 08:50:57PM +0300, Nikos Vassiliadis wrote:
 Gary,
 
  This will probably help you, it has many nice one-liners.
 
 http://sed.sourceforge.net/grabbag/tutorials/sed1line.txt
 
 Nikos


Aww, you found my stash:)  But as I said, up-queue, I'm
overdue to upgrade  forgetten skills since BSD-4.2.

gary


-- 
  Gary Kline  [EMAIL PROTECTED]   www.thought.org  Public Service Unix
  http://jottings.thought.org   http://transfinite.thought.org

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sed question...

2007-09-25 Thread Giorgos Keramidas
On 2007-09-25 11:28, Gary Kline [EMAIL PROTECTED] wrote:
 Thank you!  This will help me de-code that sed one-liner that was
 evidently written by a sedexpert. The linux pages help further, but
 I've found some soild tutorials.  

Hi Gary.

A word of caution there...

If you plan to use GNU/Linux manpages for learning sed(1) be _very_
cautious for GNU/Linux-specific parts.  There are subtle, yet possibly
important differences between GNU/Linux sed and BSD sed.

But I'm sure you know that already, right? :-)

Cheers, Giorgos

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sed question...

2007-09-25 Thread Gary Kline
On Wed, Sep 26, 2007 at 01:21:48AM +0300, Giorgos Keramidas wrote:
 On 2007-09-25 11:28, Gary Kline [EMAIL PROTECTED] wrote:
  Thank you!  This will help me de-code that sed one-liner that was
  evidently written by a sedexpert. The linux pages help further, but
  I've found some soild tutorials.  
 
 Hi Gary.
 
 A word of caution there...
 
 If you plan to use GNU/Linux manpages for learning sed(1) be _very_
 cautious for GNU/Linux-specific parts.  There are subtle, yet possibly
 important differences between GNU/Linux sed and BSD sed.
 
 But I'm sure you know that already, right? :-)
 


Sure; that's why, especially when it comes to quasi-common
untilities, I believe in the ``keep it simple, sir'' philosophy:)

gary




 Cheers, Giorgos
 

-- 
  Gary Kline  [EMAIL PROTECTED]   www.thought.org  Public Service Unix
  http://jottings.thought.org   http://transfinite.thought.org

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sed question...

2007-09-25 Thread Pollywog
On Tuesday 25 September 2007 22:21:48 Giorgos Keramidas wrote:


 A word of caution there...

 If you plan to use GNU/Linux manpages for learning sed(1) be _very_
 cautious for GNU/Linux-specific parts.  There are subtle, yet possibly
 important differences between GNU/Linux sed and BSD sed.


I am generally aware that sometimes there are differences between the 
utilities used with Linux and their BSD counterparts.  As for the differences 
themselves, do they exist because someone has reinvented the utilities or 
is there perhaps another reason?
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sed question...

2007-09-25 Thread Giorgos Keramidas
On 2007-09-25 22:49, Pollywog [EMAIL PROTECTED] wrote:
 On Tuesday 25 September 2007 22:21:48 Giorgos Keramidas wrote:
  A word of caution there...
 
  If you plan to use GNU/Linux manpages for learning sed(1) be _very_
  cautious for GNU/Linux-specific parts.  There are subtle, yet
  possibly important differences between GNU/Linux sed and BSD sed.

 I am generally aware that sometimes there are differences between the
 utilities used with Linux and their BSD counterparts.  As for the
 differences themselves, do they exist because someone has reinvented
 the utilities or is there perhaps another reason?

The history and evolution of the BSD command-line tools and their GNU
counterparts is not exactly 'linear'.  Some of the BSD features were
implemented first in BSD sources, and then where reimplemented from
scratch in the GNU toolchain.  Others were implemented first in the GNU
tools and were ported to BSD.

Sometimes, when porting a certain feature from GNU tools to the BSD
world there were 'conflicts', i.e. an option letter was already taken,
or the way GNU tools did something didn't fit the existing BSD way of
life so it was retrofitted to the traditional BSD standards, to avoid
POLA violations for example.

It's not easy to answer precisely what the reasons for all the possible
differences are, but we can at least try if you have a particular
difference in mind :-)

- Giorgos

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


sed question[s]

2007-09-24 Thread Gary Kline
Hi,

I could probably do at least part of this with an ed shell
script, but sed is probaly more standard.  (I may have asked this
before, years back: FWIW. Anyhow, don't see it in my
~/Mail/freebsd files.)

How can I automagically delete from $1,155d  AND from the 
25th line from the bottom to the last line with sed?  I spent
hours last night, by-hand saving web files; now I want to get rid
of the cruft from them.  

Again, (I think) something like % sed -e 1,$155d  filefoo 
[[yes?]], but then the last part stumps me.


tthanks, y'all,

gary


-- 
  Gary Kline  [EMAIL PROTECTED]   www.thought.org  Public Service Unix
  http://jottings.thought.org   http://transfinite.thought.org

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sed question[s]

2007-09-24 Thread Giorgos Keramidas
On 2007-09-24 14:07, Gary Kline [EMAIL PROTECTED] wrote:
 Hi,
 I could probably do at least part of this with an ed shell
 script, but sed is probaly more standard.  (I may have asked this
 before, years back: FWIW. Anyhow, don't see it in my
 ~/Mail/freebsd files.)
 
 How can I automagically delete from $1,155d  AND from the 
 25th line from the bottom to the last line with sed?  I spent
 hours last night, by-hand saving web files; now I want to get rid
 of the cruft from them.  
 
 Again, (I think) something like % sed -e 1,$155d  filefoo 
 [[yes?]], but then the last part stumps me.

sed commands like 'd' take a pair of 'addresses'.

I don't know what $1,155 means, but if you mean delete the first 155
lines, then this should be easy:

sed -e '1,155d'

The second part (deleting the last 25 lines) may be slightly trickier,
as you don't know that you are going to hit end-of-file until you
actually hit it, so some sort of bufferring is required :-/

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


sed question...

2007-09-24 Thread Gary Kline
My earlier post about deleting the first N lines was answered by
this one-liner site {below}.   I wasn't including any
redirection; doing so finally resolved the problem.  Now I need
to delete every line from the 19th or so to the last line.
Question one, can anybody explain the following syntax?  What do
P, D ba represent, in other words?


 # delete the last 10 lines of a file
 sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1
 sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2


Question two, can sed do its thing inline?

thanks in advance,

gary


-- 
  Gary Kline  [EMAIL PROTECTED]   www.thought.org  Public Service Unix
  http://jottings.thought.org   http://transfinite.thought.org

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sed question...

2007-09-24 Thread Howard Goldstein
Gary Kline wrote:
   My earlier post about deleting the first N lines was answered by
   this one-liner site {below}.   I wasn't including any
   redirection; doing so finally resolved the problem.  Now I need
   to delete every line from the 19th or so to the last line.
   Question one, can anybody explain the following syntax?  What do
   P, D ba represent, in other words?
 
 
  # delete the last 10 lines of a file
  sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1
  sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2
 
 
   Question two, can sed do its thing inline?

Wouldn't it be easier to use  head -n 18 ?

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sed question...

2007-09-24 Thread Gary Kline
On Mon, Sep 24, 2007 at 11:07:20PM -0400, Howard Goldstein wrote:
 Gary Kline wrote:
  My earlier post about deleting the first N lines was answered by
  this one-liner site {below}.   I wasn't including any
  redirection; doing so finally resolved the problem.  Now I need
  to delete every line from the 19th or so to the last line.
  Question one, can anybody explain the following syntax?  What do
  P, D ba represent, in other words?
  
  
   # delete the last 10 lines of a file
   sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1
   sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2
  
  
  Question two, can sed do its thing inline?
 
 Wouldn't it be easier to use  head -n 18 ?
 

No, because most of these files are between 40 and 50 lines.  I
only care about the first 30 or 40; everything below has to be
deleted.  By hand, using vi, I might type :31,$d  that fixes 
that one file.   Of course, I could simply edit in 19 for
10 above.  It would be more savvy to understand the sed syntax.
-- 
  Gary Kline  [EMAIL PROTECTED]   www.thought.org  Public Service Unix
  http://jottings.thought.org   http://transfinite.thought.org

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: [OT] sed question

2004-03-15 Thread Dan Rue
On Sun, Mar 14, 2004 at 04:30:40PM -0600, Steven N. Fettig wrote:
 Sorry for posting an off-topic question to the list, but this is 
 somethin that has been driving me nuts for weeks now and I can't figure 
 it out.  I want to pass a text file through sed that replaces all 
 whitespaces with a carriage return.  I.e., if I have the file 
 my_test_text_document.txt that is a few paragraphs of writing, I want to 
 take the following input:

how about using ctrl-v to get a literal - 
if you do ctrl-v, the next keystroke will be input in literally.  so, do 

sed 's/ /ctrl-venter/'

When I do that, it types in a ^M.  That method also works well for
using sed to insert tabs. 

Dan

 
 Steve Fettig
 
 
 ___
 [EMAIL PROTECTED] mailing list
 http://lists.freebsd.org/mailman/listinfo/freebsd-questions
 To unsubscribe, send any mail to [EMAIL PROTECTED]
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: [OT] sed question

2004-03-15 Thread Parv
in message [EMAIL PROTECTED],
wrote Warren Block thusly...

 ...sed on other systems does handle \n and other literals in
 substitutions.  It's annoying enough that I just use Perl instead.
 
 perl -pe 's/ /\n/g' my_test_text_document.txt
 
 which actually would be better as
 
 perl -pe 's/\s./\n/g' my_test_text_document.txt
^
^

Why do you have '.' after '\s'?  Did you mean '+' instead?


  - Parv

-- 

___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: [OT] sed question

2004-03-15 Thread Warren Block
On Mon, 15 Mar 2004, Parv wrote:

 in message [EMAIL PROTECTED],
 wrote Warren Block thusly...
 
  perl -pe 's/\s./\n/g' my_test_text_document.txt
 ^
 ^

 Why do you have '.' after '\s'?  Did you mean '+' instead?

Oops--you're correct.  \s+ for one or more whitespace characters.

-Warren Block * Rapid City, South Dakota USA
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


[OT] sed question

2004-03-14 Thread Steven N. Fettig
Sorry for posting an off-topic question to the list, but this is 
somethin that has been driving me nuts for weeks now and I can't figure 
it out.  I want to pass a text file through sed that replaces all 
whitespaces with a carriage return.  I.e., if I have the file 
my_test_text_document.txt that is a few paragraphs of writing, I want to 
take the following input:

I have just written five paragraphs of absolute jibberish and wish that 
I could get sed to work the way that I want.  Oh how this question has 
plagued me!

And have sed output:
I
have
just
written
five
paragraphs
of
absolute
jibberish
and
... you get the point.
I can't figure out what the newline character is... I've tried \n \r \, 
etc. with no avail.  I run the following:

sed 's/[ ]/\n/g' my_test_text_document.txt

and the output never has a newline added regardless of what I have 
substituted \n with.  I have also used  instead of ' and that hasn't 
helped...
Sorry for the question, but I'd really appreciate the help!

Steve Fettig

___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: [OT] sed question

2004-03-14 Thread Matthew Seaman
On Sun, Mar 14, 2004 at 04:30:40PM -0600, Steven N. Fettig wrote:
 Sorry for posting an off-topic question to the list, but this is 
 somethin that has been driving me nuts for weeks now and I can't figure 
 it out.  I want to pass a text file through sed that replaces all 
 whitespaces with a carriage return.  I.e., if I have the file 
 my_test_text_document.txt that is a few paragraphs of writing, I want to 
 take the following input:
 
 I have just written five paragraphs of absolute jibberish and wish that 
 I could get sed to work the way that I want.  Oh how this question has 
 plagued me!
 
 And have sed output:
 I
 have
 just
 written
 five
 paragraphs
 of
 absolute
 jibberish
 and
 ... you get the point.
 
 I can't figure out what the newline character is... I've tried \n \r \, 
 etc. with no avail.  I run the following:
 
 sed 's/[ ]/\n/g' my_test_text_document.txt
 
 and the output never has a newline added regardless of what I have 
 substituted \n with.  I have also used  instead of ' and that hasn't 
 helped...
 Sorry for the question, but I'd really appreciate the help!

sed(1) can do it, but it's cleaner and simpler to use tr(1):

% cat foo
I have just written five paragraphs of absolute jibberish and wish that 
I could get sed to work the way that I want.  Oh how this question has 
plagued me!
% tr -s ' ' '\n'  foo
I
have
just
written
five
paragraphs
of
absolute
jibberish
and
wish
that
I
could
get
sed
to
work
the
way
that
I
want.
Oh
how
this
question
has
plagued
me!

Cheers,

Matthew

-- 
Dr Matthew J Seaman MA, D.Phil.   26 The Paddocks
  Savill Way
PGP: http://www.infracaninophile.co.uk/pgpkey Marlow
Tel: +44 1628 476614  Bucks., SL7 1TH UK


pgp0.pgp
Description: PGP signature


Re: [OT] sed question

2004-03-14 Thread Warren Block
On Sun, 14 Mar 2004, Steven N. Fettig wrote:

 I can't figure out what the newline character is... I've tried \n \r \,
 etc. with no avail.  I run the following:

 sed 's/[ ]/\n/g' my_test_text_document.txt

From the sed man page:

2.   The escape sequence \n matches a newline character embedded in
  the pattern space.  You can't, however, use a literal newline
  character in an address or in the substitute command.

I think this is a BSD thing, and sed on other systems does handle \n and
other literals in substitutions.  It's annoying enough that I just use
Perl instead.

perl -pe 's/ /\n/g' my_test_text_document.txt

which actually would be better as

perl -pe 's/\s./\n/g' my_test_text_document.txt

-Warren Block * Rapid City, South Dakota USA
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: [OT] sed question

2004-03-14 Thread Bill Campbell
On Sun, Mar 14, 2004, Warren Block wrote:
On Sun, 14 Mar 2004, Steven N. Fettig wrote:

 I can't figure out what the newline character is... I've tried \n \r \,
 etc. with no avail.  I run the following:

 sed 's/[ ]/\n/g' my_test_text_document.txt

From the sed man page:

2.   The escape sequence \n matches a newline character embedded in
  the pattern space.  You can't, however, use a literal newline
  character in an address or in the substitute command.

I think this is a BSD thing, and sed on other systems does handle \n and
other literals in substitutions.  It's annoying enough that I just use
Perl instead.

I thought it was the other way around, that the gnu version of
sed accepts the standard ``C'' escape sequences, but the FreeBSD
version doesn't.  I always use the gnu tools configured with the
program-prefix='g' option, and referenced as ``gsed'' rather than
depending the the native ``sed'' doing what I expect.

Bill
--
INTERNET:   [EMAIL PROTECTED]  Bill Campbell; Celestial Software LLC
UUCP:   camco!bill  PO Box 820; 6641 E. Mercer Way
FAX:(206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676
URL: http://www.celestial.com/

``Never chastise a Windows user...just smile at them kindly as you would a
disadvantaged child.'' WBM
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: [OT] sed question

2004-03-14 Thread Warren Block
On Sun, 14 Mar 2004, Rob Ellis wrote:

 This works with sed in /bin/sh and ksh:

 sed -e 's/  */\
 /g' my_test_text_document.txt

 I.e., escape an actual newline.

I used to do that, or include an actual newline in a script, but it just
seems wrong from maintainability and readability standpoints.

It seems slightly better to get a newline in a shell variable and then
use that.

-Warren Block * Rapid City, South Dakota USA
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


sed question

2003-02-16 Thread How Can ThisBe
Not directly FreeBSD question, however. Is it possible with sed (or awk)
to turn this:
i/in/1 2/3 4 5 6
into
i/in/1 2/3456

The same syntax would also need to work on:
i/in/1 2/x y z  (result would be i/in/1 2/xyz)
i/in/1 2  (result would be i/in/12)

The closest I have gotten it to is:
i/in/1 2/3 4 56 with sed s/\(.*\) /\1/g

(which will work, just needs to be repeated. I do not know awk at all btw)


_
Hotmail now available on Australian mobile phones. Go to  
http://ninemsn.com.au/mobilecentral/hotmail_mobile.asp


To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-questions in the body of the message


stupid sed question

2002-11-07 Thread Mathew Kanner
Hello,
I've been going nuts trying to figure out how to embed a
newline in sed, and the man page just doesn't mean anything to me.

What I would like is
echo abc | sed -e's,b,\n'  to get
a
c

Of course, the above doesn't work and I'm looking for an
alternative.  This is freebsd's sed so I don't need a general
solution.

Thanks,
--Mat

-- 
The Brain: We're going to a place where the sun never sets, the size
of your wallet matters, and actors and actresses slave all day!
Pinky: We're going to Denny's?

To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-questions in the body of the message



Re: stupid sed question

2002-11-07 Thread Miguel Mendez
On Thu, 7 Nov 2002 15:46:25 -0500
Mathew Kanner [EMAIL PROTECTED] wrote:

Hi,

   I've been going nuts trying to figure out how to embed a
 newline in sed, and the man page just doesn't mean anything to me.
 
 What I would like is
   echo abc | sed -e's,b,\n'  to get
   a
   c

How about echo abc | tr 'b' '\n' ?

Cheers,
-- 
Miguel Mendez - [EMAIL PROTECTED]
GPG Public Key :: http://energyhq.homeip.net/files/pubkey.txt
EnergyHQ :: http://www.energyhq.tk
NetBSD :: Unix without hype

To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-questions in the body of the message



Re: stupid sed question

2002-11-07 Thread Paul A. Scott
On 11/7/02 12:46 PM, Mathew Kanner [EMAIL PROTECTED] wrote:
 What I would like is
 echo abc | sed -e's,b,\n'  to get
 a
 c

A script of the form:

echo abc | sed -e 's,b,\
,'

will work if the newline is escaped with a backslash and the remainder of
the sed substitute is on the next line.

If you run directly from the command line (not in a shell script) then you
need two backslashes, the first one escapes the backslash for the
interactive shell.

Paul A. Scott
mailto:pscott;skycoast.us
http://skycoast.us/pscott/


To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-questions in the body of the message



Re: stupid sed question

2002-11-07 Thread Paul A. Scott
 How about echo abc | tr 'b' '\n' ?

tr substitutes characters only, while sed can work on arbitrary strings and
patterns. My guess is that the example was a simplified expression of a more
general requirement, in which case sed is appropriate and therefore,

echo aaabbbccc | sed -e 's,bbb,\
,'

will yield

aaa
ccc

as expected, while tr would give

aaa


ccc

which is almost certainly not wanted.

(Reminder: you need \\ if typing directly on the command line, or a single \
if entered in a script.)

Paul A. Scott
mailto:pscott;skycoast.us
http://skycoast.us/pscott/


To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-questions in the body of the message



Re: stupid sed question

2002-11-07 Thread philipp
http://www.faqs.org/faqs/editor-faq/sed/
section 4.6

on Do, Nov 07, 2002 at 03:46:25pm -0500, Mathew Kanner wrote:
 Hello,
   I've been going nuts trying to figure out how to embed a
 newline in sed, and the man page just doesn't mean anything to me.
 
 What I would like is
   echo abc | sed -e's,b,\n'  to get
   a
   c
 
   Of course, the above doesn't work and I'm looking for an
 alternative.  This is freebsd's sed so I don't need a general
 solution.
 
   Thanks,
   --Mat
 
 -- 
 The Brain: We're going to a place where the sun never sets, the size
 of your wallet matters, and actors and actresses slave all day!
 Pinky: We're going to Denny's?
 
 To Unsubscribe: send mail to [EMAIL PROTECTED]
 with unsubscribe freebsd-questions in the body of the message

--
regards

philipp [EMAIL PROTECTED]

To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-questions in the body of the message



Re: stupid sed question

2002-11-07 Thread Mathew Kanner
On Nov 07, Paul A. Scott wrote:
  How about echo abc | tr 'b' '\n' ?
 
 tr substitutes characters only, while sed can work on arbitrary strings and
 patterns. My guess is that the example was a simplified expression of a more
 general requirement, in which case sed is appropriate and therefore,

Yes, exactly.

Thanks for the help!

--Mat
-- 
Brain: Are you pondering what I'm pondering?
Pinky: I think so, Brain, but if the plural of mouse is mice, wouldn't
the plural of spouse be spice?

To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-questions in the body of the message