help-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Kam-Hung Soh
Subject: Re: sed or awk under XP in batch file (DOS box) - print $1 lines then delete $1 lines from a file
Date: Fri, 02 May 2008 10:32:22 +1000
User-agent: Opera Mail/9.27 (Win32)

On Fri, 02 May 2008 09:13:35 +1000, John Bartley K7AAY <john.bartley@gmail.com> wrote:

On May 1, 4:02 pm, b...@proulx.com (Bob Proulx) wrote:
John Bartley K7AAY wrote:
> I need to print $1 lines from a file, and then delete that number of
> lines.
> $1 has been derived in the prior line with
> wc -l sourcefile.txt | awk '{$1 /= 4 ; $1 = int($1) ; print $1 }'

That all looks okay.  But I would probably personally do it all in the
shell.  Try this:

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

> I've tried numerous awk and sed statements, a la:

> sed -e -n "$1,p" sourcefile.txt > list.1
> sed -i "$1d" sourcefile.txt
> sed $1q list.txt > list.1 & sed -i $1d sourcefile.txt
> awk "{(FNR < $1); print}" sourcefile.txt > list.1

Try this:

  l=$(( $(wc -l < sourcefile.txt) / 4 ))
  sed --in-place "1,${l}d" sourcefile.txt

Bob

Dangit, those don't work as you expected with XP and GNUwin32

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

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

sed --in-place "1,${l}d" sourcefile.txt
sed: -e expression #1, char 7: extra characters after command


To set the output of a command to a variable in cmd.exe, use this hack:

for /f %i in ('"<command>"') do set VARIABLE=%i

So for your problem, here's a solution:

for /f %i in ('"wc -l sourcefile.txt"') do set /a LINES=%i/4
split -l %LINES% sourcefile.txt

"set /a" instructs cmd.exe to evaluate a numerical expression.

PS. John, I replied to your original message in alt.msdos.batch.nt (I was reading newsgroups in alphabetical order).

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog";>Software Salariman</a>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]