-3

I have the following code which successfully reads the line of the file that I want:

tail -n 9 myfile | awk 'NR==1'

although I do not want that this writes anything in my script. So I tried to assign a parameter into it, but it doesn't work in this way:

this=tail -n 9 myfile | awk 'NR==1'

Eventually, I want to read the second argument, which is a number, by ${1}. Could you tell me how can I do that?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
aaa
  • 161
  • 1
  • 1
  • 8
  • 1
    Please [edit] your question to provide concise, testable sample input and expected output as there's several different things you might be trying to do. Also, specifically try to clarify what you mean by `I tried to assign a parameter into it,` ("it" being what?) and `I want to read the second argument which is a number by ${1}` (the second argument to what?). – Ed Morton May 02 '19 at 19:26
  • @BenjaminW. - I could be wrong of course but I don't think that's what this question is about (or not all it's about anyway). `I want to read the second argument which is a number by ${1}` makes no sense if all the OP wants to do is save command output in a variable. – Ed Morton May 02 '19 at 19:30
  • @EdMorton I understand it's a two-parter: a) get command output into a variable and b) parametrize command with a positional parameter. I'll add a second duplicate for that, would that make sense? – Benjamin W. May 02 '19 at 19:39
  • @BenjaminW. I honestly don't know what it is the OPs asking yet. Could be passing using a shell arg as an awk variable, could be printing a specific field, etc. or could be something completely different. I also don't know if she really wants to operate on the last 9 lines of a file or some variable number passed as an arg or just the 9th-last line, etc.. So far the actual question is anyone's guess. – Ed Morton May 02 '19 at 19:41
  • @EdMorton I've reopened. I have a strong suspicion it's a mix of a few evergreens, though ;) – Benjamin W. May 02 '19 at 19:43
  • I wonder, is the underlying question "how to get the nth-to-last line of a file"? – Benjamin W. May 02 '19 at 19:44
  • 1
    It could be but that `I want to read the second argument, which is a number, by ${1}` has me scratching my head. I wonder if the OP wants to output the Xth field from the Yth line? I agree it'll probably end up being closed as a dup of something(s) but at least the OP might get the chance to learn how to ask a question :-). – Ed Morton May 02 '19 at 19:46

1 Answers1

0

It sounds like you just want to capture the output in a variable. Thus you can do this:

awkOutput=$(tail -n 9 myfile | awk 'NR==1')

and later you cant print it out

echo $awkOutput
EJK
  • 12,332
  • 3
  • 38
  • 55