Re: sed or awk under XP in batch file (DOS box) - print $1 lines then delete $1 lines from a file

[email protected] (Bob Proulx)
Newsgroups gmane.comp.gnu.utils
Message-ID <[email protected]>
John Bartley K7AAY wrote:
> > >   l=$(( $(wc -l < sourcefile.txt) / 4 ))
> > >   sed --in-place "1,${l}d" sourcefile.txt

> On May 1, 4:13 pm, John Bartley K7AAY <[email protected]>
> rebutted:
> 
> > Dangit, those don't work as you expected with XP and GNUwin32

XP!  Sorry.  I didn't see that in your subject line.  I would have
suggested something different then.  But actually I don't know how to
do it on MS and so will need to defer to others.  Sorry.

> > echo $(( $(wc -l < sourcefile.txt) / 4 ))
> > The system cannot find the file specified.

That is POSIX shell syntax.  If you have bash or ksh installed it can
do it from the command line.  I am using single quotes '...' here but
you will need to figure out how to convert them to double quotes for
XP.  I can't use double quotes in the shell because the $vars would be
expanded too soon.

  sh -c 'echo $(( $(wc -l < sourcefile.txt) / 4 ))'

And therefore perhaps something like this long one-liner.

  sh -c 'sed --in-place "1,$(( $(wc -l < sourcefile.txt) / 4 ))d" sourcefile.txt'

But if you don't have a POSIX standard shell available (e.g. bash or
ksh or ash or dash or posh or other) then it won't work.

> > l=$(( $(wc -l < sourcefile.txt) / 4 ))
> > The system cannot find the file specified.

Darn command.com!  Blech!

> > sed --in-place "1,${l}d" sourcefile.txt
> > sed: -e expression #1, char 7: extra characters after command
> 
> And, OBTW, this works when I stuff the numeric value of $1 into the
> second line... but I can't seem to pass the value into that second
> line. Ideas?

Sorry.  I don't know XP well enough to suggest a solution.

Good luck!
Bob
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.