From owner-freebsd-questions@FreeBSD.ORG Fri Sep 15 21:44:42 2006 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id BD70A16A407 for ; Fri, 15 Sep 2006 21:44:42 +0000 (UTC) (envelope-from j65nko@gmail.com) Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.171]) by mx1.FreeBSD.org (Postfix) with ESMTP id 23CC143D4C for ; Fri, 15 Sep 2006 21:44:41 +0000 (GMT) (envelope-from j65nko@gmail.com) Received: by ug-out-1314.google.com with SMTP id m2so224005uge for ; Fri, 15 Sep 2006 14:44:41 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=LEmQzBgUnAN52545kglNX2y46Kvwhn3iVccKHM/dIgEidfDQah3PendqLRaIRdsQHFd16SNnku8qnwhhR/mItTm7QxFhOT92mp7+qeAIZzu8YhKET4HBuk9ElylnwkC9sxio4lCYn0QiZnqcsbyVWoUQDCmqn4q1sTvUUOm6rL4= Received: by 10.67.100.17 with SMTP id c17mr5649514ugm; Fri, 15 Sep 2006 14:44:40 -0700 (PDT) Received: by 10.67.86.14 with HTTP; Fri, 15 Sep 2006 14:44:40 -0700 (PDT) Message-ID: <19861fba0609151444j48fbfa0fpd30345758f64455a@mail.gmail.com> Date: Fri, 15 Sep 2006 23:44:40 +0200 From: J65nko To: freebsd-questions@freebsd.org In-Reply-To: <45094622.7010803@uni-mainz.de> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <45094622.7010803@uni-mainz.de> Subject: Re: OT: awk/sed: how to use a variable in an address range? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 15 Sep 2006 21:44:42 -0000 On 9/14/06, O. Hartmann wrote: [snip] > To keep a small shell script portable I use awk for separating an ASCII > file from a home brewn scientific model software. The datasets of the > output is enclosed by > > /begin_data_set_##/ > . > . > . > /end_data_set_##/ > > ## is a two-digit counter, but not necessesaryly equidistant. > > I would like to separate the file contaning all datasets via awk or sed > into appropriate files - this is my intention, but I failed. > > the simplest way - in theory and in my limitit ability of using sed or > awk - is to print all lines between the (sed/awk) addresses > > /begin_data_set_##/ > ... > /end_data_set_##/ > > but this does not work due to i cannot use variables in the address > range specifiers neither in awk nor in sed like this: > > awk -v nc=$NUMBER '/\/begin_data_set_nc\//,/\/end_data_set_nc\// { > do-something-in-awk}' $input_file > $output_file_$NUMBER > > nc in this example is set to the counter of the desired dataset. > > I would like to use SED or AWK only due to portability reasons. [snip] You have to prefix the variable with "$" and use double quotes instead of single quotes. The shell will expand a variable within double quotes, but one within single quotes $ cat data /start_1/ This is dataset 1 /end_1/ /start_2/ This is dataset 2 /end_2/ /start_3/ This is dataset 3 /end_3/ $ cat sed_extract NR=$1 sed -ne "/\/start_$NR\//,/\/end_$NR\//p" data $ sh -vx sed_extract 3 NR=$1 + NR=3 sed -ne "/\/start_$NR\//,/\/end_$NR\//p" data + sed -ne /\/start_3\//,/\/end_3\//p data /start_3/ This is dataset 3 /end_3/ $ sh -vx sed_extract 2 NR=$1 + NR=2 sed -ne "/\/start_$NR\//,/\/end_$NR\//p" data + sed -ne /\/start_2\//,/\/end_2\//p data /start_2/ This is dataset 2 /end_2/ You were close ;)