2

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?

Community
  • 1
  • 1
RSFalcon7
  • 2,241
  • 6
  • 34
  • 55
  • 4
    Evidently, what you are doing wrong is trying to extract line 0 of the file. `sed` considers the first line to be line 1. – rici Mar 25 '14 at 02:33

2 Answers2

1

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
RSFalcon7
  • 2,241
  • 6
  • 34
  • 55
0

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
Jotne
  • 40,548
  • 12
  • 51
  • 55
  • I don't understand the script, the result is not the one expected. Either way the solution of the other question absolutely works, the problem was that I starting at line zero which apparently is invalid in `sed` – RSFalcon7 Mar 25 '14 at 15:12
  • If you post some data, and the expected result, it would be more easy for us to help and understand. – Jotne Mar 25 '14 at 15:25
  • @rici already solve it on the comments, I was expecting an answer from him so I can accept it. There is data in the another question that I referenced, also a working solution in the question (the only problem was the parameter) – RSFalcon7 Mar 25 '14 at 17:35