Reputation: 2311
Possible duplicate: Bash tool to get nth line from a file
I need to select the nth line a file, this line is defined be the variable PBS_ARRAYID
The accept solution in the another question (link above) is:
sed 'NUMq;d' job_params
I'm trying to adapt for the variable like (actually I try lots of stuff, but is the one that makes more sense):
sed "${PBS_ARRAYID}q;d" job_params
But I get the following error:
sed: -e expression #1, char 2: invalid usage of line address 0
What am I doing wrong?
Upvotes: 2
Views: 1975
Reputation: 2311
Your solution is correct:
sed "${PBS_ARRAYID}q;d" job_params
The only problem is that sed
considers the first line to be line 1 (thanks rici), so PBS_ARRAYID must be in range [1,X], where X is the number of lines on the input file, or:
wc -l job_params
Upvotes: 1
Reputation: 41446
Here is an awk
example.
Lets say we have this file:
cat file
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
Then we have theses variable:
var="four"
number=2
Then this awk
gives:
awk '$0~v {f=NR} f && f+n==NR' v="$var" n="$number" file
6 six
Upvotes: 0