|
Home > Archive > Unix Shell > October 2006 > printing line range using sed
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
printing line range using sed
|
|
|
| Hi there. First time poster to this group (though not to Usenet)
I am trying to print out a range of lines, say from 5-20, using sed.
research tells me that sed '5,20!d' is successful. I have tested it,
works a treat.
However, my problem is that the range is variable, so I'm wanting to
write something like:
sed '$var_from,$var_to!d' file_name
which isn't working. I've tried braces, ` 's, around the variables
names, e.g. i've tried
sed '`echo $var_from`,`echo $var_to`!d' (yes, I'm new to scripting.)
If anyone could give me a pointer, all the examples thrown up on the web
use literal values.
Cheers,
Mitch.
| |
| Spiros Bousbouras 2006-10-22, 7:19 pm |
| Mitch wrote:
> Hi there. First time poster to this group (though not to Usenet)
>
> I am trying to print out a range of lines, say from 5-20, using sed.
>
> research tells me that sed '5,20!d' is successful. I have tested it,
> works a treat.
>
> However, my problem is that the range is variable, so I'm wanting to
> write something like:
>
> sed '$var_from,$var_to!d' file_name
>
> which isn't working. I've tried braces, ` 's, around the variables
> names, e.g. i've tried
>
> sed '`echo $var_from`,`echo $var_to`!d' (yes, I'm new to scripting.)
>
> If anyone could give me a pointer, all the examples thrown up on the web
> use literal values.
sed -n $var1,${var2}p file
| |
| Chris F.A. Johnson 2006-10-22, 7:19 pm |
| On 2006-10-22, Mitch wrote:
> Hi there. First time poster to this group (though not to Usenet)
>
> I am trying to print out a range of lines, say from 5-20, using sed.
>
> research tells me that sed '5,20!d' is successful. I have tested it,
> works a treat.
>
> However, my problem is that the range is variable, so I'm wanting to
> write something like:
>
> sed '$var_from,$var_to!d' file_name
>
> which isn't working. I've tried braces, ` 's, around the variables
> names, e.g. i've tried
>
> sed '`echo $var_from`,`echo $var_to`!d' (yes, I'm new to scripting.)
Variables inside single quotes are not expanded; use double quotes:
sed "$var_from,$var_to!d" file_name
However, faster would be:
sed -n -e "$var_from,$var_to p" -e "$var_to q" file_name
as it will exit after the last line you want.
Other methods include awk:
awk "NR == $var_from,NR == $var_to" file_name
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| Spiros Bousbouras 2006-10-22, 7:19 pm |
| Mitch wrote:
> Mitch wrote:
> [...]
>
> Just a note, I'm not restricted to sed, so would be interested in all
> the different ways this can be done. Unix being famous for the hundred
> different ways you can do everything...
This could become a long thread ;-)
head -n $end file | tail +$beg
awk NR==$beg,NR==$end file
| |
|
| Spiros Bousbouras wrote:
> Mitch wrote:
>
>
> sed -n $var1,${var2}p file
>
Thanks!
| |
|
| Spiros Bousbouras wrote:
> Mitch wrote:
>
>
> This could become a long thread ;-)
>
I'd hate to be the one to start it off ;)
| |
|
| Chris F.A. Johnson wrote:
[...]
>
> sed "$var_from,$var_to!d" file_name
>
[...]
This worked a treat, thanks, however this doesn't:
sed "`expr $halfWayPoint + 1`,`expr $noOfLines - 1`!d" >>
/tmp/attempt2.1.tmp2
I'm not sure yet if I require the noOfLines var to be -1, however I go
up to halfway, insert some text, then need to carry on from where I left
off. I'm sure I could bodge this with incrementing/decrementing the
variable before I use it in sed, but as I'm learning, I was curious as
to how this would be done.
Thanks for your patience.
Mitch.
| |
| Ed Morton 2006-10-23, 7:21 pm |
| Mitch wrote:
> Chris F.A. Johnson wrote:
> [...]
>
> [...]
>
> This worked a treat, thanks, however this doesn't:
>
> sed "`expr $halfWayPoint + 1`,`expr $noOfLines - 1`!d" >>
> /tmp/attempt2.1.tmp2
>
> I'm not sure yet if I require the noOfLines var to be -1, however I go
> up to halfway, insert some text, then need to carry on from where I left
> off. I'm sure I could bodge this with incrementing/decrementing the
> variable before I use it in sed, but as I'm learning, I was curious as
> to how this would be done.
With awk, this:
sed "$var_from,$var_to!d"
would be this (see question 24 in the comp.unix.shell FAQ,
http://home.comcast.net/~j.p.h/cus-faq-2.html#24, for details on why
this is one correct way to pass the value of shell variables to awk
scripts), assuming you don't care about performance:
awk -v from="$var_from" -v to="$var_to" 'NR==from,NR==to'
while what you appear to be trying to do with this:
sed "`expr $halfWayPoint + 1`,`expr $noOfLines - 1`!d"
would be this, again assuming you don't care about performance:
awk -v from="$var_from" -v to="$var_to" 'NR==(from+1),NR==(to-1)'
Regards,
Ed.
| |
| Loki Harfagr 2006-10-24, 1:17 pm |
| Le Sun, 22 Oct 2006 15:59:14 -0700, Spiros Bousbouras a écrit_:
> Mitch wrote:
>
>
> This could become a long thread ;-)
>
> head -n $end file | tail +$beg
>
> awk NR==$beg,NR==$end file
And, in case 'tail' or 'head' is sloppy but you have 'tac'
you can try these :-)
------
tac | tail -n $end | tac| tail -n $(( 1+ $end - $sta ))
head -n $end | tac| head -n $(( 1+ $end - $sta )) |tac
------
example of use, make them functions :
------
$ type _Excerpt1
_Excerpt1 is a function
_Excerpt1 ()
{
end=$3;
sta=$2;
tac "${1}" | tail -n $end | tac | tail -n $(( 1+ $end - $sta ))
}
$ type _Excerpt2
_Excerpt2 is a function
_Excerpt2 ()
{
end=$3;
sta=$2;
head -n $end "${1}" | tac | head -n $(( 1+ $end - $sta )) | tac
}
------
------
$ seq 100 | _Excerpt1 - 5 10
5
6
7
8
9
10
------
------
$ seq 100 | _Excerpt2 - 5 10
5
6
7
8
9
10
------
Well, that may help somebody some day :-)
| |
|
| Ed Morton wrote:
> Mitch wrote:
>[...]
>
> would be this (see question 24 in the comp.unix.shell FAQ,
> http://home.comcast.net/~j.p.h/cus-faq-2.html#24, for details on why
> this is one correct way to pass the value of shell variables to awk
> scripts), assuming you don't care about performance:
> [...]
Thanks for that, I read it, re-read it... I must confess I haven't read
the FAQ yet. I will though, honest... 
Thanks for all your help, I have completed what it was I was trying to
do, so am very happy. Onto my next task now... 
|
|
|
|
|